Overloading Assignment Operator

In C++, the assignment operator (=) is used to assign a value to a variable. However, sometimes the default behavior of the assignment operator may not be suitable for certain user-defined types. To address this, C++ allows overloading of the assignment operator, enabling custom behavior when assigning values to objects of user-defined classes.

Why Overload the Assignment Operator?

Overloading the assignment operator allows customizing the behavior of assignment for objects of a class. This is particularly useful when deep copying is required, or when additional actions need to be performed during assignment.

  • Deep vs. Shallow Copy: The default assignment often performs a shallow copy, meaning it copies the references to member variables, not the actual values themselves. If these member variables are pointers or dynamic arrays, it can lead to issues like dangling pointers or memory leaks. Overloading the assignment operator lets you control the copying process and ensure a deep copy, where the actual values of member variables are copied.
  • Custom Logic: You might have specific tasks you want to perform during assignment, such as resource management or data validation. Overloading the assignment operator gives you the flexibility to implement this custom logic.
				
					#include <iostream>

class Point {
public:
    int x, y;

    // Default constructor
    Point() : x(0), y(0) {}

    // Overloaded assignment operator
    Point& operator=(const Point& rhs) {
        if (this != &rhs) { // Self-assignment check (optional)
            x = rhs.x;
            y = rhs.y;
        }
        return *this;
    }

    // Function to display point coordinates
    void display() const {
        std::cout << "(" << x << ", " << y << ")" << std::endl;
    }
};

int main() {
    Point p1(3, 5);
    Point p2;

    p2 = p1; // Calls the overloaded assignment operator

    p1.display(); // Output: (3, 5)
    p2.display(); // Output: (3, 5)
}

				
			
				
					// output //
(3, 5)
(3, 5)
				
			

Explanation:

  • The Point class has member variables x and y to store point coordinates.
  • The overloaded assignment operator (operator=) takes a constant reference (const Point& rhs) to the source object.
  • Inside the operator function:
    • A self-assignment check (if (this != &rhs)) is optional but prevents unnecessary copying when assigning the same object to itself.
    • The member variables of the object (*this) are assigned the values from the source object (rhs).
    • The function returns a reference to the current object (*this) to allow for chaining assignments (e.g., p1 = p2 = p3).

Overloading the assignment operator in C++ allows customization of assignment behavior for user-defined types. By providing a custom implementation, we can ensure that assignments behave as expected for objects of our classes, enabling more intuitive and efficient code.Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India