Sign in to follow this  
DOS4dinner

How do you declare multiple objects at once? (C++)

Recommended Posts

Is there some way to declare, maybe in a for loop or something, multiple class objects? I.E. For(i=1, i != 10, i++) { objectclass object(i); } Is this possible, or do you have to manually declare each object?

Share this post


Link to post
Share on other sites
Quote:
Original post by DOS4dinner
For(i=1, i != 10, i++)
{
objectclass object(i);
}

Assuming you meant to write ; instead of , inside the for loop, you would only have created nine objects: 1 2 3 4 5 6 7 8 9.

Anyway, I suggest using a vector:

#include <vector>

class objectclass { /* your definition of the class here */ };

int main()
{
std::vector<objectclass> v;
for (int i = 0; i < 10; ++i)
v.push_back(objectclass());
}

Share this post


Link to post
Share on other sites

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