Effective C++条款05:了解C++默默编写并调用哪些函数

时间:2025-05-05 17:06:50
class Empty{};

class Empty{
Empty(){};
Empty(const Empty& rhs){};
~Empty(){};
Empty& operator=(const Empty& rhs){}; };

只有当生成的代码合法证明它有意义时编译器才会为class生成operator=

template<class T>
class NameObject{
private:
string& nameValue;
const T objectValue;
};

如上,对于引用和const,编译器拒绝赋值

还有一种情况:某个基类将拷贝赋值函数声明为private,则编译器拒绝为派生类生成拷贝赋值函数,毕竟编译器所生成的拷贝赋值函数想象可以处理基类部分,所以无法在派生类调用基类的拷贝赋值函数

请记住

编译器默认为class生成默认构造函数,拷贝构造函数,拷贝赋值函数,析构函数