Thread: 2D array of objects

  1. #1
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265

    2D array of objects

    How do I create a 2D array of objects and initialize it?

    Thanks.
    gavra.

  2. #2

  3. #3
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    |:
    I meant something like this:
    Code:
    class MyClass
    {
    .....
    ...
    };
    
    MyClass arr2d[...][...]; // how do I declare this 2D array?
    and how do I allocate and initialize (using the constructor) each element?
    gavra.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gavra
    and how do I allocate and initialize (using the constructor) each element?
    Something like this:
    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)}
    };
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    nevremind.. I had to declare the array as a pointer:
    MyClass* arr2d[x][y];
    thanks anyway.. (:
    gavra.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by gavra View Post
    nevremind.. I had to declare the array as a pointer:
    MyClass* arr2d[x][y];
    thanks anyway.. (:
    You are aware that's a 2-D array of pointers, not a pointer to an array or an array of objects themselves?

  7. #7
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    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.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gavra
    What do you mean? I thought I did it right since it's working.. (arr2d[i][j]->Function())
    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?

    Quote Originally Posted by gavra
    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.
    It is likely that MyClass has a user defined destructor but not a default constructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Here is what I did:
    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));
    		}
    Should I free the memory manualy?
    gavra.

  10. #10
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by gavra View Post
    Should I free the memory manualy?
    Yes! Delete the pointer and set it to null in the deconstructor, always practice safe computing! :P

  11. #11
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    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.

  12. #12
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    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.

  13. #13
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    I don't understand how to access my array from the class.. (?..)
    gavra.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  15. #15
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing/pointing to a 2d array
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 10-28-2005, 10:16 PM
  2. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM