c++类中成员的构造顺序
先是父类各成员的初始化, 各成员的默认构造函数, 再调用父类的构造函数,然后是各成员的初始化, 各成员的默认构造函数,然后是自己的构造函数。
class a{
public:
a()
{
cout << "construct a" << endl;
}
};
class combinebase
{
public:
combinebase()
{
cout << "construct combinebase" << endl;
}
};
class combine: public combinebase
{
public:
combine(int i)
{
cout << "construct combine" << endl;
}
private:
a d;
};
输出结果为:
construct combinebase
construct a
construct combine
java中先初始化基类和子类的static成员,然后类似