Preventing compilers from defining copy constructo

2019-06-20 07:40发布

Is there a way by which we can prevent compilers from defining copy constructors, operator = overload for C++ classes.

标签: c++ class
7条回答
迷人小祖宗
2楼-- · 2019-06-20 08:08

Defining? Well, yes. They are always declared (explicitly by you or implicitly by the compiler), but they are only defined by the compiler when/if you actually use them. Don't use them - and the compiler will not define them.

Of course, if by "prevent compilers from defining ...", you mean "prevent compilers from successfully defining...", i.e. if you want to make the implicit definition attempt fail at compile time, then you can achieve that by adding non-copy constructible and/or non-assignable subobject to your class (like a base or member with private copy-constructor and private assignment operator, for example).

查看更多
登录 后发表回答