I know the C++ rule of thumb when you add cctor, dtor or op= to your class, you need to add the other two too to make you class work properly under all circumstances.
Are there any case when you don't need to provide all the three, just one or two of them?
Why don't the C++ require you to add them all if you add one of them to your class?
EDIT1:
You mentioned examples when you not only don't need some of them, but you don't want to have them so you want to make them private or protected. But you still need to write them in your code even with empty bodies.
The only valid reason for me not having them all when you make a class polimorphic by adding a virtual destructor with empty body. But as soon as you write something in the destructor's body, you should consider writing something in the body of the cctor and op= too.
I'm looking for examples when you don't need to write all 3 methods and you can omit some of them without causing bugs in your class even if a newbie are using your class. :)
Unlike other operators, the compiler will provide a default public assignment operator for your class if you do not provide one. This assignment operator does memberwise assignment (which is essentially the same as the memberwise initialization that default copy constructors do).
The Copy constructor and the assignment operators are used to initializing one object to another object. The main difference between them is that the copy constructor creates a separate memory block for the new object. But the assignment operator does not make new memory space.
The compiler provides a copy constructor if there is no copy constructor defined in the class. Bitwise copy will be made if the assignment operator is not overloaded. Copy Constructor is used when a new object is being created with the help of the already existing element.
When we don't provide an implementation of copy constructor (and assignment operator) and try to initialize an object with the already initialized object of the same class then copy constructor gets called and copies members of class one by one in the target object.
One case in which you may want to use a destructor without a copy constructor or assignment operator is when developing a polymorphic class, in which case you want a virtual
destructor to allow deallocation through a base class pointer. These classes often will support copy constructors in order to facilitate the "virtual clone" idiom. However, they rarely have assignment operators, since polymorphic classes are usually heap-allocated and referenced only through pointers, in which case direct assignment almost always leads to slicing.
Observed classes (those that report their lifetimes to another class) require all constructors and destructor, but not op=. C++ doesn't require them because it would be completely unnecessary- we, the programmers, know best.
In addition, you may want destructor but not the other two if you have a non-copyable class.
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