Difference between Copy constructors and Assignment operator

Copy constructor is a special kind of constructor which initializes an object using another object. Compiler always provides a default copy constructor but if needed it can be over-ridden by programmer to meet the requirements.

Assignment operator is basically assigning the new values to already created object. In this case object initialization doesn’t happen as it is already done.

For eg:

class Base{
};

Base b1;
Base b2 = b1; /* Copy constructor as initialization also needs to done */
Base b3;
b3 = b2; /* Assignment operator as object is already created */
(more…)
Difference between Copy constructors and Assignment operator Read More

Difference between Constructors and Destructors

Constructor

Constructors are special member functions of a class which is used to initialize the object in desired way. Constructors has the same name as that of class. There could be multiple constructors defined in a class which could be selected based on invoking conditions.

Types of constructor

  • Default constructor – Any constructor without argument is called default constructor.
  • Parameterized constructor – Any constructor in which argument can be passed is called Parameterized constructor.
  • Copy constructor – This is a special kind of constructor which uses an existing object to create a new object.

Destructor

Destructor is a special member function of a class which is used to delete the object. Destructor also has the same name as that of class prefixed with “~” sign. In any class, one could define only one destructor. Destructor doesn’t take any argument.

(more…)
Difference between Constructors and Destructors Read More

Difference between Copy constructor vs Move constructor

 

Copy ConstructorsMove Constructors
Copy constructors takes lvalue reference as an argument.Move constructors takes rvalue reference as argument.
Copy constructors create a new object from passed object by copying each and every item into a new memory location.Move constructors create a new object by using as much memory from passed object.
Since, copy constructors use a lot of new memory allocation(for new object). Hence, copy constructors gives bad performance compared to move constructorsSince, move constructors use most of memory blocks from passed object. Hence, move constructors gave better performance compared to copy constructors.
Since, copy constructors doesn’t make any change to passed object. Hence, passed object can be used after copy operations also.Since, move constructors utilizes memory blocks from passed object. Hence, passed object can’t be used after move operations.

For more information related to Move constructors click here

Difference between Copy constructor vs Move constructor Read More

Move Constructors explained with simple example


In C++11 (Introduction to C++11) , move constructors are added which can improve any code’s performance drastically. Typically a move constructor is same as copy constructor which will create a new instance based on the passed object. But, the idea behind move constructor is to avoid memory reallocation and use as many memory from the passed original object because the original object is about to be deleted as it has been provided as a temporary object.

Since, in move constructors we avoid memory reallocation while creating new instance while will improve the code’s performance as we know memory allocation is quite costly.
While implementing move constructors we need to take care of one important point to ensure that the original object can be correctly destroyed.

Let’s take an example to see how this memory reallocation is avoided in case of move constructor.

(more…)

Move Constructors explained with simple example Read More