Overloaded = Operator Example

Overloaded Assignment Operator in C++


1. About Assignment Operator Overloading

By default, any class will have a compiler given copy constructor and assignment operator. In order to avoid shallow copy one will overload the copy constructor as well as Assignment Operator. The below picture shows the syntax for the ‘= Operator’ overloading:

Assignment Operator Overloading Syntax

Assignment Operator Overloading Syntax

The left-hand side of the assignment operator will make a call to the Operator =. Hence, it will pass the value on the right-hand side as the argument. The overloading function gets the argument as a const reference. This will guarantee that the right side operand never changes. In this article, we will see how to overload an assignment operator.

2. CPoint3d Class

Below are the private members of the CPoint3d class which we will experiment using the overloaded assignment operator.

This class CPoint3d is initialized through two kinds of the constructor. One is a custom-defined default constructor, which sets all the data members of the CPoint3d class as zero. The other one takes all required values as arguments and set the internal members. The below code shows both the constructors below:

The class has a print method, which prints all the coordinate values on the screen. The function is shown below:

3. Overloading the Assignment Operator

The overloaded ‘operator =’ shown in the below picture:

Overloaded = Operator Example

Overloaded = Operator Example

In the above code, the left-hand side of the assignment calls overloaded operator= function. The right-hand side value is going as an argument to the function. The implementation copies the values from the passed-in argument to the calling object p1.

Note, the code has a conditional check, this == &rhs. This conditional check is useful to avoid the self-assignment. The keyword this gives the calling object’s address which is nothing but the address of the object on the left side. Since we received the argument to the operator overloading as a reference, &rhs in our case gives the address of the right-hand side object. So, using this address-check, we can avoid self-assignments. Returning the object is useful for making assignment as part of expressions as shown in the above picture. Below is the code (For copy paste):

4. Supporting const as RValue

The implementation below shows how to handle the assignment for the constant values. In our case, the implementation supports constant values for integer. Say, for example, the below implementation makes the P1 = 73 possible. It assigns all the P1 coordinate values to 73.

5. Testing the class

In the below example, we create two objects. We set some sample values to object pt1 by invoking the constructor with three arguments. We set the object pt2 with default values through constructor with no arguments. After setting up the object, we copy the content of the object pt1 to pt2 using our overloaded operator. The code is below:

In the next snippet, self-assignment and the multiple assignments are tested. Note,  the conditional check against the this object  avoids the self-assignment. In our example pt3 = pt3 tests the self-assignment and the statement pt1 = pt2 = pt3 tests the multi-assignment.

In the final snippet, we check our second version of the overload. Recall, this version takes a number as right hand side value. This means the left side is an object, and it calls the operator. Right side of the operator is a number. It goes as an argument to the operator. Below is the code for it:

Below is the output of executing the full code:

Overloaded Operator Example

Overloaded Operator Example

6. Complete Source Code Example

You can read other operator overloading examples here:

  1. Overload ‘+ Operator’ in C++
  2. Overload ‘++ Operator’ in C++

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.