This is the Member Initializer List. Its purpose is that when the QWidget* parent parameter is passed into the Widget's constructor, the QWidget member in the Widget class will be initialized with parent, that is, QWidget(parent). The advantage of using member initialization list is that it can reduce copying, for example:
This is because C++ stipulates that the initialization of member variables occurs before entering the constructor, that is, the member QWidget will be initialized before entering the constructor. Here, it is initialized by the default constructor. Therefore, in the above code, QWidget is initialized twice by , plus the = operator (Assign Operator) is called once. The cost of these operations is higher than using the member initialization list. So generally speaking, try to use member initialization lists. That is:
Initialization of base class constructor.
This is calling the constructor of the base class QWidget, parent is the parameter passed into the base class constructor
Defining constructors outside the class and base class constructors
Initialization list, display calling the parent class constructor in the initialization list
Basic syntax of c++, initialization list.
This is the Member Initializer List. Its purpose is that when the QWidget* parent parameter is passed into the Widget's constructor, the QWidget member in the Widget class will be initialized with parent, that is, QWidget(parent).
The advantage of using member initialization list is that it can reduce copying, for example:
This is because C++ stipulates that the initialization of member variables occurs before entering the constructor, that is, the member QWidget
will be initialized before entering the constructor. Here, it is initialized by the default constructor. Therefore, in the above code, QWidget is initialized twice by
, plus the = operator (Assign Operator) is called once. The cost of these operations is higher than using the member
initialization list. So generally speaking, try to use member initialization lists. That is:
Call the constructor of the parent class. This is written on C++ Primer
Constructor initialization list, the first column can use the constructor of the parent class.
is to call the constructor of the parent class.