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

Started by
2 comments, last by DOS4dinner 15 years, 4 months ago
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?
Advertisement
Would an array (or dynamic array, such as std::vector<>) not work?
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());}
I see. Thanks!

This topic is closed to new replies.

Advertisement