I have a class called Image:
when I want to instanciate it i just do this:Code:class Image { ... public: ..... }
Image *i = new Image();
what I want to do is hav an array of those classes. How would i do that using the new keyword?
I have a class called Image:
when I want to instanciate it i just do this:Code:class Image { ... public: ..... }
Image *i = new Image();
what I want to do is hav an array of those classes. How would i do that using the new keyword?
Image *i = new Image[20];
...
...
delete [] i;
I guess i should have mentioned this. im passing parameters to the constructor:
Image *i = new Image(filename, flags);
should i be doing something like this:
?Code:Image **i; *i = new (Image *) Image[30]; for (int x = 0; x < 30; x++) { i[x] = new Image(filename, flags); }
This seems to work:
Code:Image* (*p)[3] = new Image*[2][3]; p[0][0] = new Image(x, y); cout<<p[0][0]->member_name<<endl;
For the delete, I think you have to cycle though each array position with a double for loop, and in the inner loop delete each pointer, e.g.:
delete p[outer][inner];
and in the outer loop delete each row of the array:
delete [] p[outer];
EDIT:I think that last line is wrong. After you delete all the individual pointers, and both for loops have finished, then I think you should do this instead:
delete [] p;
Last edited by 7stud; 02-26-2005 at 01:02 PM.
OK, thanks for the help. Ill try it out tomorrow.
If you don't know the number of columns ahead of time, e.g.
Image* (*p)[3]
then you will have to do something like this:
If rows=2 and cols=3, that sets up a structure like this:Code:int rows, cols; cout<<"Enter the rows and colums: \n"; cin>>rows>>cols; Image*** p = new Image**[rows]; for(int i=0; i<rows; i++) { p[i] = new Image*[cols]; } p[0][0] = new Image(x, y); cout<<p[0][0]->member_name<<endl;
EDIT:Code:***p ------>**p0--------->*p0, *p1, *p2 **p1--------->*p0, *p1, *p2
Deleting: in this case you have multiple pointers to arrays: ***p points to the first row of an array, and **p0 and **p1 point to the first element of an array. So, you need to start off deleting all the individual pointers using two for loops, and at the bottom of the outer for loop, you need to delete the arrays pointed to by **p0 and **p1,
delete [] p[outer_index];
Finally, after both for loops terminate, you need to delete ***p:
delete [] p;
Hopefully someone will verify that.
Last edited by 7stud; 02-26-2005 at 01:10 PM.
If you want to eliminate one level of indirection by storing objects in your array rather than pointers to objects, you can do this:
That would set up a structure like this:Code:int rows, cols; cout<<"Enter the rows and colums: \n"; cin>>rows>>cols; Image** p = new Image*[rows]; for(int i=0; i<rows; i++) { p[i] = new Image[cols]; } p[1][0] = Image(x, y); cout<<p[1][0].member_name<<endl;
Deleting:Code:**p-------->*p0----->Image0, Image1, Image2 *p1----->Image0, Image1, Image2
Code:for(int i=0; i<rows; i++) { delete [] p[i]; } delete [] p;
I dont think this is exactly what i need. Here is basically what im trying to do:
there is a pointer to an image object and what i want is an array of those pointers.Code:**p-------->*p0----->Image0 *p1----->Image1 *p2----->Image2 *p3----->Image3
Code:**p-------->*p0----->Image0, Image1, Image2 *p1----->Image0, Image1, Image2Describe the differences between those two arrays.Code:**p------->*p0----->Image0 *p1----->Image1 *p2----->Image2 *p3----->Image3
Last edited by 7stud; 02-26-2005 at 04:50 PM.