Sign in to follow this  

putting objects in an arry?

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

This topic is 3313 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this