How do I create a 2D array of objects and initialize it?
Thanks.
How do I create a 2D array of objects and initialize it?
Thanks.
gavra.
|:
I meant something like this:
and how do I allocate and initialize (using the constructor) each element?Code:class MyClass { ..... ... }; MyClass arr2d[...][...]; // how do I declare this 2D array?
gavra.
Something like this:Originally Posted by gavra
Code:class MyClass { public: MyClass(int x, int y) : x(x), y(y) {} private: int x; int y; }; // ... MyClass arr2d[2][2] = { {MyClass(0, 1), MyClass(2, 3)}, {MyClass(4, 5), MyClass(6, 7)} };
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
nevremind.. I had to declare the array as a pointer:
MyClass* arr2d[x][y];
thanks anyway.. (:
gavra.
What do you mean? I thought I did it right since it's working.. (arr2d[i][j]->Function())
When I tried this: MyClass arr2d[..][..] the compiler show me an error (something with the constructor..)
and I want to initialize the array using a loop, not as laserlight has shown.
gavra.
The bad part is that you would probably be doing manual memory management this way. You did set those pointers to point to MyClass objects, right?Originally Posted by gavra
It is likely that MyClass has a user defined destructor but not a default constructor.Originally Posted by gavra
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Here is what I did:
Should I free the memory manualy?Code:Brick* bricks[bh][bw]; for(int i=0;i<bh;i++) for(int j=0;j<bw;j++) { bricks[i][j] = new Brick((float)(35*i+250.0/1024.0*SCREEN_WIDTH), (float)(18*j+200.0/768.0*SCREEN_HEIGHT)); }
gavra.
you mean like this:
? [=Code:for(int i=0;i<bh;i++) for(int j=0;j<bw;j++) { delete bricks[i][j]; bricks[i][j] = NULL; }
gavra.
Yes, always delete pointers that have been created on the heap. It's also safe practice to set them to null (point to nothing) also, otherwise it could point to anything! Although my personal opinion is to do this in the deconstructor belonging to your class, that way when your object is destroyed, you can ensure that the pointer to that object is also destroyed.
Last edited by legit; 06-07-2009 at 05:27 AM.
I don't understand how to access my array from the class.. (?..)
gavra.
Which array, from what class? Are you saying you have a class that needs to refer to an array or objects of it's own class? That doesn't sound like a correct solution [generally speaking, although it is not technically impossible to come up with something that does]. Please describe a bit more of what you are actually doing [big picture, "puncture" rather than "wheelnuts" level]
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I have a class and I want to create a 2D array consist of objects of this class - I did it as shown above but someone told me that I should not declare the array as a pointer.
now I want to delete the allocated memory - I did it but someone said that I should do it from the destructor.
so how am I supposed to declare the array? and how do I delete the allocated memory using the destructor?
thanks (:
gavra.