Keypoint : 调用父类的构造函数(一般为有参构造函数),初始化类中的成员。
C++ primer 5th edition:
Remember
When creating an object of a derived class, a program first calls the base-class constructor and then
calls the derived-class constructor. The base-class constructor is responsible for initializing the inher-
ited data members. The derived-class constructor is responsible for initializing any added data mem-
bers. A derived-class constructor always calls a base-class constructor. You can use the initializer list
syntax to indicate which base-class constructor to use. Otherwise, the default base-class constructor
is used.
When an object of a derived class expires, the program first calls the derived-class destructor and
then calls the base-class destructor.
Member Initializer Lists
A constructor for a derived class can use the initializer list mechanism to pass values along to a base-
class constructor. Consider this example:
derived::derived(type1 x, type2 y) : base(x,y) // initializer list
{
...
}
Here, derived is the derived class, base is the base class, and x and y are variables used by the base-
class constructor. If, say, the derived-class constructor receives the arguments 10 and 12, this mecha-
nism then passes 10 and 12 on to the base-class constructor defined as taking arguments of these
types. Except for the case of virtual base classes (see Chapter 14, “Reusing Code in C++”), a class
can pass values back only to its immediate base class. However, that class can use the same mecha-
nism to pass back information to its immediate base class, and so on. If you don’t supply a base-class
constructor in a member initializer list, the program uses the default base-class constructor. The
member initializer list can be used only with constructors.