Jump to content
  • Advertisement
Sign in to follow this  
DOS4dinner

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

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

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!