#include <iostream>
using namespace std;
class Base
{
public:
Base()
{
cout << "Base constructor " << endl;
}
Base(const Base &b)
{
cout << "Base copy constructor " << endl;
}
Base& operator=(const Base& b)
{
cout << "Base copy assignment " << endl;
}
};
class Derive : public Base
{
public:
};
int main()
{
Derive d1, d2;
d1 = d2;
return 0;
}
输出:
Base constructor
Base constructor
Base copy assignment
如果
class Derive : public Base
{
public:
Derive()
{
cout << "Derive constructor " << endl;
}
Derive(const Derive& d)
{
cout << "Derive copy constructor " << endl;
}
Derive& operator=(const Derive& d) //派生类若不显示定义copy assignment,d1 = d2 就会调用基类的
{
cout << "Derive copy assignment " << endl;
}
};
输出:
Base constructor
Derive constructor
Base constructor
Derive constructor
Derive copy assignment