#include<iostream>
using namespace std;
class Base
{
public:
Base(){cout<<"base"<<endl;}
Base(Base &b){cout<<"base(&)"<<endl;}
virtual ~Base(){cout<<"~base"<<endl;}
virtual void mf(){cout<<"B::fun()"<<endl;}
};
class Derived:public Base
{
public:
Derived(){cout<<"Derived"<<endl;}
Derived(Derived &d){cout<<"Derived(&)"<<endl;}
~Derived(){cout<<"~Derived"<<endl;}
virtual void mf(){cout<<"D::fun()"<<endl;}
};
void func()
{
Derived d;
Derived d1 = d;//先调用B类的构造函数,再调用D类中的拷贝构造函数
}
int main()
{
func();
getchar();
return 0;
}
见注释部分
结果:
其它资料:C++在单继承、多继承、虚继承时,构造函数、复制构造函数、赋值操作符、析构函数的执行顺序和执行内容