Using the STL, you can have an array (or vector) of objects, thus:
class Alien; // define your Alien
std::vector<Alien> aliens(20); // create 20 aliens (this could instead be just a vector of sf::Sprites or sf::RectangleShapes or whatever if the only thing the aliens need is their appearance)
// ...
// draw all aliens
for (auto& alien : aliens)
window.draw(alien);
If you're unable to use C++11 (or later) for whatever reason, to iterate the aliens (to draw them as above), you'd need to access them via iterator:
for (std::vector<Alien>::iterator it = aliens.begin(); it != aliens.end(); ++it)
window.draw(*it);
or index:
for (unsigned int i = 0u; i < aliens.size(); ++i)
window.draw(aliens[i]);
For more information about some of the STL containers (e.g. vectors), have a look at the following pages:
VECTOR:
http://www.cplusplus.com/reference/vector/vector/
http://en.cppreference.com/w/cpp/container/vector
ARRAY:
http://www.cplusplus.com/reference/array/array/
http://en.cppreference.com/w/cpp/container/array
Long time, but, since I am optimizing now my game - I was trying to do so by changing way of iterating.
So, due to my Visual Studio profiler - when using auto c++11 vs using index, auto construction is slower by about 5% (and I fps are going down from ~2000 to 1500) - I am drawing about 120 sprites...
Using the STL, you can have an array (or vector) of objects, thus:
class Alien; // define your Alien
std::vector<Alien> aliens(20); // create 20 aliens (this could instead be just a vector of sf::Sprites or sf::RectangleShapes or whatever if the only thing the aliens need is their appearance)
// ...
// draw all aliens
for (auto& alien : aliens)
window.draw(alien);
If you're unable to use C++11 (or later) for whatever reason, to iterate the aliens (to draw them as above), you'd need to access them via iterator:
for (std::vector<Alien>::iterator it = aliens.begin(); it != aliens.end(); ++it)
window.draw(*it);
or index:
for (unsigned int i = 0u; i < aliens.size(); ++i)
window.draw(aliens[i]);
For more information about some of the STL containers (e.g. vectors), have a look at the following pages:
VECTOR:
http://www.cplusplus.com/reference/vector/vector/
http://en.cppreference.com/w/cpp/container/vector
ARRAY:
http://www.cplusplus.com/reference/array/array/
http://en.cppreference.com/w/cpp/container/array