7 years ago.

C++ class initialization orders ?

I am evaluating a Semtech LoRa code for its SCPI command parser. When I build the code, it throws 4 warnings:

sx127x.cpp

SX127x::SX127x(PinName mosi, PinName miso, PinName sclk, PinName cs, 
                PinName rst, PinName dio_0, PinName dio_1) :
                m_spi(mosi, miso, sclk),  
								m_cs(cs), 
								reset_pin(rst), 
								dio0(dio_0), dio1(dio_1)
{
    reset_pin.input();
    m_cs = 1;
    m_spi.format(8, 0);
    m_spi.frequency(3000000);
    
    init();
}

warnings

compiling sx127x.cpp...
SX127x/sx127x.cpp(22): warning:  #1299-D: members and base-classes will be initialized in declaration order, not in member initialisation list order
  								m_cs(cs), 
SX127x/sx127x.cpp(24): warning:  #1299-D: members and base-classes will be initialized in declaration order, not in member initialisation list order
  								dio0(dio_0), dio1(dio_1)
SX127x/sx127x.cpp(24): warning:  #1299-D: members and base-classes will be initialized in declaration order, not in member initialisation list order
  								dio0(dio_0), dio1(dio_1)
SX127x/sx127x.cpp(25): warning:  #1361-D: function "mbed::FunctionPointerArg1<R, void>::FunctionPointerArg1(R (*)()) [with R=void]" (declared at line 67 of "mbed/./platform/FunctionPointer.h") was declared "deprecated"
  {
SX127x/sx127x.cpp: 4 warnings, 0 errors
linking...
Program Size: Code=55880 RO-data=6264 RW-data=260 ZI-data=2788  

What do they mean? How can I remove these warnings ? Should I declare DigitalOut/DigitalIn for these pins (CS/RST/DIO0/DIO1) before these constructor ?

2 Answers

7 years ago.

In principle the warning isn't really important in this case, however of course it is nicer if it compiles without warnings. If I remember correctly you need to declare them in your .h file in the same order as you put them in your .cpp file in line 3-6 here in your piece of code.

7 years ago.

Hi Kai,

You can read this item from the C++ FAQ. Base class and data member order initialization follow declaration order, not constructor initialization list order:

class Foo; 
class Bar; 

Foo make_foo(); 
Bar make_bar(const Foo& f);

struct Qux { 
    // not valid, _bar will be initialized before _foo with a not initialized 
    // instance of _foo
    Qux() : _foo(make_foo()), _bar(make_bar(_foo)) { } 

private: 
   Bar _bar;
   Foo _foo;
}