When resizing an array of pointers to objects, how do I define the array, and how do I copy the objects from 1 array to another.
defining in header of Parent:
Child *init;
then in the Parent constructor, allocating memory for 10 Child objects: Child = new *init [10];
and finally a function for resizing:
1 2 3 4 5 6 7 8 9 10 11
Child *temp [20];
for ( i=0 ; i<10 ; i++ )
{
temp[i] = init[i]; // should I use assignment operator
temp[i](init[i]); // or overloaded copy constructor?
}
delete [] init;
Child init = new Child[20];
//then same as above, for (i=0....)
This is just the core of everything, I can figure the rest of it myself, I just need the defining and basic logic.
Thanks
Parent and Child are classes.
init would be the array that we use for the program, temp would be used for resizing. I know it is incorrect that is why I am asking how to do this - define and resize (copy).
Copy constructor is a constructor that accepts another object.
OOOOOhhh. OK, let's start from the beggining. In the first line of code, there are 2 errors. Really big ones!! First of all, you cant assign ANYTHING to a class. Second of all, arrays aren't types! So, switch the names
Child
and
init
to get init=new Child*[10]. Then, in the next snippet, you CAN'T use a constructor (ANY constructor!) on an already constructed object!!! I suggest this: http://www.cplusplus.com/doc/tutorial/classes/