复制对象时勿忘记其每一个成分
如果自己不定义copy构造函数和copy assignment操作函数的话,编译器提供的copy构造函数和copy assignment操作符函数会自动对其基类部分进行copy构造和copy assign,但是如果自己定义了copy构造函数和copy assignment操作函数的话,那么就要在这两个函数中对基类进行复制。
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Base { public: std::string name; };
class Derived: public Base { public: int age; Derived(const Derived &another): Base(another), age(another.age) {}; Derived& operator=(const Derived &another) { Base::operator=(another); age = another.age; } }
|