一个类的构造函数包括:
相关的赋值操作符:
上述方法或者操作符,在某些情况下编译器都会自动生成:
一个类,成员类型,删除赋值构造,这个类的赋值构造也会默认删除。
class Base {
public:
Base() {
cout << "base ctor()\n";
}
Base(const Base& b) {
cout << "base copy ctor()\n";
}
Base(Base&& b) noexcept {
cout << "base move ctor()\n";
}
Base& operator=(const Base& b) {
cout << "base copy =\n";
return *this;
}
Base& operator=(Base&& b) noexcept {
cout << "base move =\n";
return *this;
}
~Base() {
cout << "base de-ctor\n";
}
};
class Derive: public Base {
public:
Derive() {
cout << "derive: default\n";
}
Derive(const Derive& d): Base{d} {
cout << "derive: copy ctor\n";
}
// Derive& operator= (const Derive& d) {
// cout << "derive: copy =\n";
// return *this;
// }
// Derive(Derive&& d): Base{move(d)} {
// cout << "derive: move ctor\n";
// }
// Derive& operator= (Derive&& d) {
// cout << "derive: move =\n";
// return *this;
// }
~Derive() {
cout << "derive: de-ctor\n";
}
};