I was wondering what is the proper way to initialize a std::array
member of the class in the constructor, when the initial array values are parameters to the constructor?
More specifically, consider the following example:
class Car {
public:
Car(const std::string& color, int age): color_(color), age_(age) {}
// ...
private:
std::string color_;
int age_;
};
class ThreeIdenticalCars {
private:
std::array<Car, 3> list;
public:
ThreeIdenticalCars(const std::string& color, int age):
// What to put here to initialize list to 3 identical Car(color,age) objects?
{}
};
Obviously one way is to write list({Car(color,age), Car(color,age), Car(color,age)})
, but this clearly does not scale if we wanted 30 identical cars instead of three.
If instead of std::array
I used std::vector
the solution would have been list(3, Car(color,age)
(or list(30, Car(color, age))
but as in my problem the size of the list is known, I thought it is more correct to use std:array
.