I have a use case that my object must not be copied in any way. I have written an exaggerated complete list of copy constructor and copy assignment operator deletions below. There are so many of them that I can't make sure which ones to use, and sometimes this makes me paranoid. I don't have to write them all in my code, do I? So, in order to prevent object copying of any kind, which of them should I use?
MyClass ( MyClass &) = delete;
MyClass (const MyClass &) = delete;
MyClass ( MyClass &&) = delete;
MyClass (const MyClass &&) = delete;
MyClass operator=( MyClass &) = delete;
MyClass operator=(const MyClass &) = delete;
const MyClass operator=( MyClass &) = delete;
const MyClass operator=(const MyClass &) = delete;
MyClass & operator=( MyClass &) = delete;
MyClass & operator=(const MyClass &) = delete;
const MyClass & operator=( MyClass &) = delete;
const MyClass & operator=(const MyClass &) = delete;
MyClass && operator=( MyClass &) = delete;
MyClass && operator=(const MyClass &) = delete;
const MyClass && operator=( MyClass &) = delete;
const MyClass && operator=(const MyClass &) = delete;
MyClass operator=( MyClass &&) = delete;
MyClass operator=(const MyClass &&) = delete;
const MyClass operator=( MyClass &&) = delete;
const MyClass operator=(const MyClass &&) = delete;
MyClass & operator=( MyClass &&) = delete;
MyClass & operator=(const MyClass &&) = delete;
const MyClass & operator=( MyClass &&) = delete;
const MyClass & operator=(const MyClass &&) = delete;
MyClass && operator=( MyClass &&) = delete;
MyClass && operator=(const MyClass &&) = delete;
const MyClass && operator=( MyClass &&) = delete;
const MyClass && operator=(const MyClass &&) = delete;
When to delete copy constructor and assignment operator? Copy constructor (and assignment) should be defined when ever the implicitly generated one violates any class invariant. It should be defined as deleted when it cannot be written in a way that wouldn't have undesirable or surprising behaviour.
The copy constructor and copy-assignment operator are public but deleted. It is a compile-time error to define or call a deleted function. The intent is clear to anyone who understands =default and =delete . You don't have to understand the rules for automatic generation of special member functions.
A user-defined copy constructor is generally needed when an object owns pointers or non-shareable references, such as to a file, in which case a destructor and an assignment operator should also be written (see Rule of three).
You only need to mark a single copy constructor and copy assignment operator as delete
. The presence of the copy versions will prevent the implicit-declaration of the move constructor and move assignment operator, and declaring one form of a copy special member function suppresses the implicit-declaration of other forms.
MyClass (const MyClass&) = delete;
MyClass& operator= (const MyClass&) = delete;
Note that post-C++11, implicit-definition of the assignment operator as defaulted is deprecated and it should instead be defined as deleted.
copy constructor
MyClass (const MyClass &) = delete;
assignement operator
MyClass & operator=(const MyClass &) = delete;
These are the only copy constructors ans copy assignement operators implicitly defined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With