Copy and Move Constructors

In the life of the C++ programmer, there are 2 major interesting constructor strategies. One of them is for copping an object and the other for moving data.

The copy constructor is basic structure which receives a reference of an object. The parameter object must be constant because in the copping operation we can’t modify the values of the object. The structure of the method is:

The other method is the move constructor that receives reference of the reference object. The structure is;

this method runs when a method needs to create a copy of data of existing class

In flowing code, you may find an example code

The move constructor has to set pointers to nullptr because if old object destroys its destroy method may delete existing pointer and we can not reach the values on newly created or moved one.

To force a method to run this move controller: you have to include <memory> header file and std::move is the method to call this constructor.

Leave a Reply

Your email address will not be published. Required fields are marked *