这就是我想要做的:
class A{ public: A(){/*need to initialize something here*/} }; int main(){ A a; //OK a=A(); //not OK a=A(b); //not OK ///Only A a; needs to be allowed. return 0; }
我需要初始化一些东西,同时防止使用复制构造函数初始化对象,并且还要阻止分配给现有对象.
注意:如果我能在没有的情况下做到这一点会很好>=C++11
.
从C++ 11开始,只需删除赋值运算符:
class A{ public: A(){/*need to initialize something here*/} A& operator =(const A&) = delete; };