2D array of objects

This is a discussion on 2D array of objects within the C++ Programming forums, part of the General Programming Boards category; How do I create a 2D array of objects and initialize it? Thanks....

  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
    Registered User
    Join Date
    Mar 2007
    Posts
    416

  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
    22,125
    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)}
    };
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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
    22,125
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 06: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.

Page 1 of 2 12 LastLast
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, 11: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, 08:39 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21