C++派生类构造函数

uacs2024 / 2024-03-06 / 原文

实例

#include <iostream>
using namespace std;

class Base1 {//基类Base1,构造函数有参数
public:
    Base1(int i) { cout << "Constructing Base1 " <<i<< endl; }
};

class Base2 {//基类Base2,构造函数有参数
public:
    Base2(int j) { cout << "Constructing Base2 " <<j<< endl; }
};

class Base3 {//基类Base3,构造函数无参数
public:
    Base3() { cout << "Constructing Base3 *" << endl; }
};

class Derived: public Base2, public Base1, public Base3 {
public:
    Derived(int a, int b, int c, int d): Base1(a), Base2(b), member1(c), member2(d)//此处的次序与构造函数的执行次序无关
    {}
private:
    Base1 member1;
    Base2 member2;
    Base3 member3;
};

int main()
{
    Derived obj(1,2,3,4);
    return 0;
}

结果

Constructing Base2 2
Constructing Base1 1
Constructing Base3 *
Constructing Base1 3
Constructing Base2 4
Constructing Base3 *

根据继承顺序,先用接受到对应的值去初始化,调用基类的构造函数。然后按照子类成员声明顺序,调用相应的构造函数。