Jump to content
  • Advertisement
Sign in to follow this  

putting objects in an arry?

This topic is 3677 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well if you wanted an array of NUMBER_OF_ELEMENTS named my_array of type MyClass you would do:

MyClass my_array[NUMBER_OF_ELEMENTS];

Share this post

Link to post
Share on other sites
can i store these objects in an arry?
Yes, and there are a few different ways to do so. For example:

// A static array (like SiCrane suggested):
Room rooms[] = { a, b, c, d };

// A boost::array (which is a fairly simple wrapper around a static array):
typedef boost::array<Room, 4> rooms_t;
rooms_t rooms = { a, b, c, d };

// If you don't know how many rooms you're going to have, you'll probably want
// to use a dynamic array class of some sort, e.g. std::vector:
typedef std::vector<Room> rooms_t;
rooms_t rooms;

// If you have the Boost libraries available, you can shorten the above to:
rooms_t rooms = boost::assign::list_of(a)(b)(c)(d);

// This isn't recommended, but doing the memory management yourself would
// look something like this:
Room* rooms = new Room[4];
rooms[0] = a;
rooms[1] = b;
rooms[2] = c;
rooms[3] = d;
// ...
delete [] rooms;

As you can see, there are a number of different things you can do here. Which is the optimal solution depends on the circumstances.

Share this post

Link to post
Share on other sites
Original post by Z_of_Thule
no i mean say i have the class room, and i make 4 objects (a,b,c,d) with the classd room. can i store these objects in an arry?

Do you want these objects to be in the variables a b c d AND the array? Because

Room rooms[] = {a, b, c, d};

will COPY the objects into the array.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!