Object names containing an array

I was just wondering if an object name can be part of an array, like this

1
2
3
4
5
6
7
while(true)
{
MyClass classObject[variable];
classObject[variable].MyClassFunctions();
variable ++;
}
//(not a working code) 

Where the variable could be used to allow loops to set multiple objects.

If not then is there another way to use a loop to set up multiple objects in a class?

(I'm working with SFML and I want to set up a function that could set multiple rectangles onto the screen, so I need to set up a loop that can make multiple unique objects based on a number that is input by that function.)
Last edited on
This declaration

MyClass classObject[variable];

requires constant expression for the array size. So variable shall be declared as const.

Here

classObject[variable].MyClassFunctions();

you will get access violation because you are trying access the array outside its allocation. Acceptable indexes of the array are [0, variable - 1].
If you can not provide a const expression for the array size you can use standard container std::vector.
Last edited on
I get it, thanks, program is working now.
Topic archived. No new replies allowed.