#include <iostream>
#include <array>
class MyObject
{
public:
// No implicit default constructor
MyObject(int num) : number(num) {}
int getNumber() const { return number; }
private:
int number;
};
int main()
{
// Can use initialization lists and create the objects inside the list.
std::array<MyObject, 2> objects = { MyObject(1), MyObject(2) };
// Standard C++11 ranged based for loop to print.
for (auto& i : objects)
std::cout << i.getNumber() << std::endl;
return 0;
}
@keskiverto
YES! This is how I figured it was done. It's just that I kept getting compiler errors because I was using a single opening and closing brace. Is there a reason why you have to have 2?
@Z e r e o
Thanks. I didn't know you could do it this way.