"Members and base-classes will be initialized in declaration order, not in member initialisation list order"

28 Sep 2013

Hi,

I would like to declare some default arguments in the class constructor like so: (header file)

class foo  {       
    public:
        foo (PinName b1 = PTA13, 
               PinName b2 = PTD5, 
               PinName b3 = PTA4);
}

In the cpp file the constructor is:

foo::foo (PinName b1, PinName b2, PinName b3) 
            : _b1(b1), _b2(b2), _b3(b3) {
    // Class constructor
}

This returns the warning "Members and base-classes will be initialized in declaration order, not in member initialisation list order" What does this mean, I'm confused since the declaration order and the initialization order is the same! And is there perhaps a better way to initialize the defaults?

Thanks,

Koen

28 Sep 2013

I have had that one before, generally I just mixed it until the warning disapeared, it shouldn't be a problem further. Isn't it that _b1, _b2 and _b3 are declared in a different order?

Regarding initializing defaults, in principle this is a normal way to do it. An alternative is to make two constructors, one just requesting the three pinnames, the other one without arguments, which then uses those pins. Which one is handier depends on the situation: Then you have the option to either have all default, or all custom. Here you can make b1 and b2 different, and keep b3 the default.

Whichever is preferably depends on the situation. For example if those pins are an SPI interface it is unlikely you only want to change 2 of the 3 pins, and then you prefer an error if someone does that.

28 Sep 2013

The warning is related to the order in which _b1, _b2 and _b3 are declared in your class definition. They are probably private members of your class and declared in the .h file. This declaration order should match the order in the constructor to get rid of the warning.