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; Have you considered using a vector of vectors instead of a 2D array? For example: Code: std::vector<std::vector<Brick> > bricks(bh); for ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Have you considered using a vector of vectors instead of a 2D array? For example:
    Code:
    std::vector<std::vector<Brick> > bricks(bh);
    for (int i = 0; i < bh; ++i)
    {
        bricks[i].reserve(bw);
        for (int j = 0; j < bw; ++j)
        {
            bricks[i].push_back(Brick(
                35 * i + 250.0f / 1024.0f * SCREEN_WIDTH,
                18 * j + 200.0f / 768.0f * SCREEN_HEIGHT));
        }
    }
    You would then be able to access bricks[i][j] as a Brick object instead of a Brick pointer, and you would no longer need to worry about managing the memory for the dynamic array.
    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

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want a 2D array of objects, you do that the same as anything else:
    Code:
    Brick my_array[11][15]
    If you want to call the constructors in a loop, then you can certainly use a 2D array of pointers, as you had above. If you do so, you will also need to call delete in a loop just the same. (If you don't have any new() calls in the constructor and aren't doing anything special in the destructor, you don't have to call the destructor here, just delete and set the pointer to NULL.)

    In either event, you can't refer to the array from the class, just as you can't refer to any objects from the class since the class has no idea about what or how many variables are created of that type (in the same way that if you type "int j" the data type int doesn't suddently know about j).

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Quote Originally Posted by tabstop
    If you want a 2D array of objects, you do that the same as anything else:
    Refer to post #7. The class does not have a default constructor. Of course, it may be the case that an appropriate fix is to provide 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

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by laserlight View Post
    Refer to post #7. The class does not have a default constructor. Of course, it may be the case that an appropriate fix is to provide a default constructor.
    Or provide an initializer list.

    But if the dimensions are variables (do we have VLA in C++?) then vector is probably the right suggestion.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Quote Originally Posted by tabstop
    Or provide an initializer list.
    Yes, that is what I originally suggested, but look at how the example initialisation is done. Even if bh and bw were compile time constants, if they are sufficiently large, one would have to resort to writing and running a script to feasibly generate the initializer list... or maybe the array will be too large to fit on the stack.

    Quote Originally Posted by tabstop
    do we have VLA in C++?
    Not in standard C++.
    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

  6. #21
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    ok thanks everyone, I used vector [=
    gavra.

Page 2 of 2 FirstFirst 12
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

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