Rule of Zero
- If a class doesn’t need to declare a destructor, it does not need to declare a copy constructor or move operators either
Rule of Three
- If we need to declare a destructor then
- we need a copy constructor
- and a copy assignment operator
Rule of Five
- Rule of Three and
- we probably need to provide a constructor to allocate memory resource
- if performance is important, we need to add move operators
Move-only class
Info
- apply when we need to declare a destructor
- but we do not want to copy the object
- e.g a class to manage network connection
- the destructor to close the connection
- we do not want to duplicate the connection
How to
- Declare on our own
- Destructor
- Move constructor
- Move assignment operator
- A constructor might be needed (to open connection e.g.)
- A default constructor might be useful to create an unconnected object
- The compiler will synthesis the copy operator as deleted
- For clarity, we can explicitly declare the copy constructor as =delete
Immovable Class
Info
These objects cannot be passed to a function
These objects cannot be returned from a function
e.g low level concurrency objects which release a lock at the end of their scope
How to
- Delete the copy constructors
- The compiler will not synthesis the move operator
- Any move or copy operation will invoke the copy constructor, which is deleted
Copy-only Class
Warning
Not recommended
Even though the move operator is deleted, the compiler might decide it is the best match