Overloading Assignment Operator

Written by Raza on. Posted in C++

While working with objects in Object Oriented Paradigm, we often want to implement user defined functionalities. C++ gives us the facility to define the use of the already existing operators by our own choice. One such operator is the assignment or equality operator ( = ). It is used for transferring the contents of one object into another object. It should be defined by the user in his class as a good programming approach and to ensure smooth execution of the program. There are some important things to be kept in mind while overloading assignment operator, described here.

Assignment operator is very similar to a copy constructor. Read how to Write a Good Copy Constructor. Their difference lies in that Copy Constructor is called at the time of initialization whereas Assignment Operator is called at the time of assignment. The following code snippets show these scenarios respectively.

</p>
<p>Vector v1 ;</p>
<p>Vector v2 = v1 ;</p>
<p>

 

</p>
<p>Vector v1, v2 ;</p>
<p>v2 = v1 ;</p>
<p>

 

If you haven’t defined the assignment operator here yourself, compiler will itself generate and execute it doing member wise copy. Again read the previous post Write a Good Copy Constructor for detail . We overload the equality operator somewhat like this.

</p>
<p>Vector&amp; Vector :: operator = (const Vector &amp;obj)</p>
<p>{</p>
<p>size = obj.size ;</p>
<p>arr = new int [size] ;</p>
<p>for (int i = 0 ; i &lt; size ; ++i)</p>
<p>arr [i] = obj.arr [i] ;</p>
<p>return *this ;</p>
<p>}</p>
<p>

The code seems good enough for now. However, look at the following scenario.

</p>
<p>Vector v1, v2, v3 ;</p>
<p>v1 = v2 ;</p>
<p>.</p>
<p>.</p>
<p>.</p>
<p>v2 = v3 ;</p>
<p>

Here, what mistake you did was that you assigned the value of v1 to v2 and after sometime, assigned v3 to v2. Notice that the previous data of v2 has been lost and the array inside has also been lost, making that memory inaccessible. So a memory leak has occurred here. Therefore in order to cater this problem, you must delete the previous array of the object before assigning a new one. The code would become like this.

</p>
<p>Vector&amp; Vector :: operator = (const Vector &amp;obj)</p>
<p>{</p>
<p>size = obj.size ;</p>
<p>delete [] arr ;</p>
<p>arr = new int [size] ;</p>
<p>for (int i = 0 ; i &lt; size ; ++i)</p>
<p>arr [i] = obj.arr [i] ;</p>
<p>return *this ;</p>
<p>}</p>
<p>

But by doing so, another problem may arise now. Look at this.

 

</p>
<p>Vector v1, v2, v3 ;</p>
<p>v1 = v2 ;</p>
<p>.</p>
<p>.</p>
<p>.</p>
<p>v1 = v1 ;</p>
<p>

You wanted to make it equal to v3 but mistakenly made it equal to v1 itself. Now what, you delete the array of v1 and then copy the data of that deleted array in its new array. So make sure in your code that you are not doing self-assignment by mistake. This would be the final shape of the code.

 

</p>
<p>Vector&amp; Vector :: operator = (const Vector &amp;obj)</p>
<p>{</p>
<p>if (this != &amp;obj)</p>
<p>{</p>
<p>size = obj.size ;</p>
<p>delete [] arr ;</p>
<p>arr = new int [size] ;</p>
<p>for (int i = 0 ; i &lt; size ; ++i)</p>
<p>arr [i] = obj.arr [i] ;</p>
<p>}</p>
<p>return *this ;</p>
<p>}</p>
<p>

In this function, you return that object by reference upon which the function is called. Keep this thing in mind. There are certain rules when we return an object by reference from a function and sometimes not. Lastly remember that assignment operator could be overloaded as many times as you wish but it must always take only one parameter.

Tags: , ,