0

Update: Thanks for the quick replies and sorry for the duplicate – I didn't know what to search on since I didn't know what that construction is called.

I've only used very, very simple C++ classes (in working with the Arduino) so excuse me if this question is too basic.

I'm trying to understand the code snippet below which is from a Qt BLE example. I've not seen a constructor like this before. Can someone tell me what it is called (style, etc. so I can look it up), and/or what it is doing?

Obviously it is initializing some instances and some variables but I don't understand why the... (they aren't parameters, so what are they?) "initializers" are outside of the curly braces.

pingpong.cpp
pingpong.h


PingPong::PingPong():
    m_serverInfo(0), socket(0), discoveryAgent(0), interval(5), m_resultLeft(0), m_resultRight(0),
    m_showDialog(false), m_role(0), m_proportionX(0), m_proportionY(0), m_serviceFound(false)
{
    m_timer = new QTimer(this);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(update()));
}
spring
  • 18,009
  • 15
  • 80
  • 160

3 Answers3

2

It's a way of initializing the members of a class in C++, called member initializer list.

Another way (mostly C++11/14/17) can be initializing the members directly in the Header file / Class declaration

Assume:

class PingPong
{
  public:
    PingPong();

  private:
    int32_t m_serverInfo{0};
    std::string m_aString{"I am a string"};
};

The timer and the connect() are used to initialize the QTimer and connect the signal timeout() emitted by the timer and connect that event to another function, called update() and member of PingPong class

madduci
  • 2,635
  • 1
  • 32
  • 51
1

That's called a member initialization list. It's necessary for members and bases that can't be default-initialized. But it's also, as here, just a convenient notation.

Note that the order in the list does not influence the actual initialization order.

The members that are initialized, are initialized in declaration order regardless of order in a member initialization list. You might however get a warning if those orders differ. Reason for weasel language: if the constructor that's used to initialize the object doesn't explicitly initialize some plain old data member (such as e.g. an int member), and that member isn't declared with a default initializer, then that member is just left uninitialized, with an indeterminate value.

A full discussion of the relevant details would however be too long to place here.

You need a good C++ textbook, and for that you can look in the SO C++ Book List.

Community
  • 1
  • 1
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • the order doesn't influence the initialization order, but some compilers (e.g. gcc), complain with a warning "out-of-order" initialization, when full warnings are enabled – madduci Jan 09 '16 at 12:06
1

It's member initializer list:

member initializer list specifies the initializers for direct and virtual base subobjects and non-static data members.

Before the compound statement that forms the function body of the constructor begins executing, initialization of all direct bases, virtual bases, and non-static data members is finished. Member initializer list is the place where non-default initialization of these objects can be specified. For members that cannot be default-initialized, such as members of reference and const-qualified types, member initializers must be specified. No initialization is performed for anonymous unions or variant members that do not have a member initializer.

The order of member initializers in the list is irrelevant: the actual order of initialization is as follows:

1) If the constructor is for the most-derived class, virtual base classes are initialized in the order in which they appear in depth-first left-to-right traversal of the base class declarations (left-to-right refers to the appearance in base-specifier lists)

2) Then, direct base classes are initialized in left-to-right order as they appear in this class's base-specifier list

3) Then, non-static data members are initialized in order of declaration in the class definition.

4) Finally, the body of the constructor is executed

songyuanyao
  • 169,198
  • 16
  • 310
  • 405