Jump to content
  • Advertisement
Sign in to follow this  
Z_of_Thule

putting objects in an arry?

This topic is 3531 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

Advertisement
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
Quote:
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;
rooms.push_back(a);
rooms.push_back(b);
rooms.push_back(c);
rooms.push_back(d);

// 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
Quote:
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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!