Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy constructor, destructor and assignment operator. When don't we need them all?

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. :)

like image 719
Calmarius Avatar asked Mar 31 '11 09:03

Calmarius


People also ask

What happens if we don't supply an assignment operator?

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).

Can assignment operator be used instead of copy constructors?

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.

In what situations a copy constructor is invoked instead of assignment operator?

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.

What happens when we do not make our own copy constructor?

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.


2 Answers

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.

like image 198
templatetypedef Avatar answered Sep 27 '22 21:09

templatetypedef


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.

like image 32
Puppy Avatar answered Sep 27 '22 20:09

Puppy