开发者

creating multiple objects for classes/structures?

开发者 https://www.devze.com 2023-04-10 21:24 出处:网络
I have a question. Is it possible to create multiple objects during run time for classes or structures?

I have a question. Is it possible to create multiple objects during run time for classes or structures?

#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
       int no;
};

int main()
{
   int i;
   for(i=0;i<4;i++)
开发者_StackOverflow   {
                   struct node s[i];
   }
   cout<<"Enter the values";
   for(i=0;i<4;i++)
   {
                   cin>>s[i].no;
   }
   cout<<"The values are:";
   for(i=0;i<4;i++)
   {
                   cout<<s[i].no<<endl;
   }
   getch();
   return 0;
}

I tried the method above , but didn't succeed . Any help would be appreciated


replace

for(i=0;i<4;i++)
{
       struct node s[i];
}

with

 struct node s[4];

the way you wrote your program will not work. You defined the node array s inside a block so it will not be visible outside of that block.

If you want to dynamically allocate the memory you have to do something like:

 struct node *s = new node[YourDesiredSize];

or if you like the c style (not recommended):

 struct node *s;
 s = (node*)malloc(YourDesiredSize * sizeof (node));

and don't forget to free the memory.


for(i=0;i<4;i++)
{
 struct node s[i];
}

Here you create an array of nodes inside a for loop. This is local and only available in the for loop. In addition this will not compile as it is written because i is not a constant expression. Even however if you used the new operator to allocate the array as before it would only be available in the for loop.

Instead you should declare your array somewhere else :

 node s[4];

This will create an array of size 4 by calling the default c'tor of node. Then your code should work.


If you want to create instances of structs or classes at runtime you need to use the new operator.

struct node* n = new n[4]; // creates an array of 4 node structs

for( int i=0; i<4; ++i )
{
  n[i]->no = i;
}

Since this is dynamically allocated memory you are responsible for freeing it when the structs are no longer needed.

delete[] n; // free dynamically allocated memory - the brackets are needed because this an array


That can be done with a native array of instances (of structures or classes) if you know the count of them, or you can used collections such as list or vector if you don't.

#include<iostream>
#include<list>

using namespace std;

struct node
{ 
    int no;
    node() { }
    node(int i) { no = i; }
};

int main()
{
    struct node * arrayOf4Nodes = new node[4];

    cout << "Enter four values: ";
    int i;
    for(i = 0; i < 4; i ++) {
        cin >> arrayOf4Nodes[i].no;
    }

    cout << "The values are:" << endl;
    for(i = 0; i < 4; i ++) {
        cout << arrayOf4Nodes[i].no << endl;
    }

    delete [] arrayOf4Nodes;

    // OR for unknown lengths

    cout << "Enter values ending with -1000 to exit: ";
    list<node> listOfNodes;
    while (true) {
        cin >> i;
        if (cin.eof() || i == -1000)
            break;
        listOfNodes.push_back(node(i));
    }

    cout << "The values are:" << endl;
    for (node n : listOfNodes) {
        cout << n.no << endl;
    }

    return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消