Initialize C++ object array within the Constructor Initializer List

278 Views Asked by At

I am trying to initialize an array of objects. This seems to fail in Keil, using the armcc compiler, using the --cpp11 flag.

The following is my class Wheel which would be used later in the Vehicle class.

class Wheel
{
public:
    Wheel(const uint8_t diameter);
private:
    uint8_t m_diameter;
};

The Vehicle class employs an array of Wheel, and these are initialized within the constructor. I would like to ideally initialize it like this:

class Vehicle
{
public:
    Vehicle();

private:
    Wheel wheels[3];
};

//in Wheel.cpp
// Constructor 
Vehicle::Vehicle()
    : wheels{Wheel(3),Wheel(4),Wheel(5)} 
{}

The other option would be to initialize everything within the constructor body, like this:

Vehicle::Vehicle(){
wheel[0] = Wheel(3);
wheel[1] = Wheel(4);
wheel[2] = Wheel(5);}

The compiler documentation mentions:

ARM Compiler supports initializer lists and uniform initialization, but the standard library does not provide an implementation of std::initializer_list. With a user-supplied implementation of std::initializer_list initializer lists and uniform initialization can be used.

How can I define an initializer list for my usecase?

0

There are 0 best solutions below