64,637
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class root
{
public:
root(int i = 0)
{
cout<<"constructor"<<endl;
}
root(root ©_object)
{
cout<<"copy function"<<endl;
}
root &operator=(const root &rhs)
{
cout<<"root operator=()"<<endl;
return *this;
}
~root()
{
cout<<"destructor"<<endl;
}
};
class child : public root
{
};
int main(void)
{
child child_a;
child child_b = child_a;
child_a = child_b;//调用了root::operator=
return 0;
}
|-------------| <<-------derived
|base |
|-------------|
|derived-base |
| |
|-------------|
声明一个派生类对象
derived md;
等价于
base mb;
derived-base mdb;
次序永远是派生类在前面
所以这个时候就自然要调用base类的构造函数构造mb
然后在derived的构造函数里面舒适化derived-base的部分