The World of Mayukh Bose

<< Back to Main Page Mayukh's world: e-mail me | about
Mayukh's World: Operator Overloading With C++ Thursday, November 23, 2017
Index
  • Introduction
  • What Operators?
  • Rules
  • Assignment Operator
  • More on Assignment
  • Arithmetic Operators
  • Arithmetic with Globals
  • Increment/Decrement
  • Operator-Assignment
  • Unary Operators
  • Relational Operators
  • Bitshift/Extraction
  • Subscript Operator
  • Function Call Operator
  • Bit and Logical Ops
  • Comma Operator
  • Pointer to Member
  • new and delete Ops
  • Credits and Thanks
  • My Free Software
  • Delphi/C++ Builder
  • Pocket PC
  • FreeSpeech Chat
  • C/C++ Freebies
  • Perl
  • Python
  • Ruby
  • C++ Operator Overloading Tutorial e-mail me

    Assignment Operator (=)

    The first operator that we will learn to overload is the assignment operator (=). This operator allows us to assign a variable to another value or variable. Of course, a copy constructor provides similar functionality as the overloaded = operator, but we will study this to lead us to bigger and greater things. We will start by redefining = to assign one complex number to another. First we modify the class definition like this:
    	  class Complex {
    	    private:
    	      double real, imag;
    	    public:
    	      Complex() { real = imag = 0; }
    	      Complex(double r, double i) { real = r; imag = i; }
    	      double GetReal(void) const { return real; }
    	      double GetImag(void) const { return imag; }
    	      Complex& operator=(const Complex& num);
    	   };
    	
    This tells the compiler that we're overloading the = operator for objects of class Complex. It also tells the compiler that this operator takes a reference to a Complex class object and returns a reference to a Complex class object. The const keyword assures the compiler that we're not going to alter the variable that is passed by reference to the function. The code will work just fine without the const keyword, but we want to be neat and clean, don't we? Next, we have to put the actual code behind the method like this:
    	  Complex& Complex::operator=(const Complex& num) {
    	    real = num.real;
    	    imag = num.imag;
    	    return *this;
    	  }
    	
    Since this method is a member of class Complex, it can access its private members. This is why it can access num.real and num.imag directly. Also note that this function is declared to return a reference to class Complex and hence we're returning *this at the bottom of the function. We will see why this is done in a moment. For now, let's test out the code and see what happens:
       	  int main(void) {
    	    Complex num1(2, 3);
    	    Complex num2;
    	    cout << "num1 is " << num1.GetReal() << " + " 
    	         << num1.GetImag() << "i" << endl;
    	    cout << "num2 is " << num2.GetReal() << " + " 
    	         << num2.GetImag() << "i" << endl;
                cout << "Assigning num1 to num2" << endl;
    	    num2 = num1;
    	    cout << "num2 is " << num2.GetReal() << " + " 
    	         << num2.GetImag() << "i" << endl;
    	    return 0;
    	  }
    	
    Running this program, we get the following output:
    	  num1 is 2 + 3i
    	  num2 is 0 + 0i
    	  Assigning num1 to num2
    	  num2 is 2 + 3i
    	
    This seems to work exactly as we expected. Now we'll examine why we were returning *this in the overloaded operator. The reason behind this is that it allows us to chain assignment operations like this:
    	  Complex a, b, c;
    	  Complex d(2, 5);
    	  a = b = c = d; // Multiple assignments
    	
    If the = operator overload method wasn't declared to return Complex&, multiple assignments like the example above wouldn't be possible.

    As we mentioned in the beginning of this page, our overloaded = operator code performs a function similar to what a copy constructor does. In fact, the compiler automatically generates a default copy constructor for you that copies data from class Complex. This does the same thing as the assignment operator we've just implemented. So does this mean that our effort is wasted? Not really -- for one thing, you now have a feel for how operator overloading works. Also, the compiler doesn't automatically generate code to allow us to assign other types of variables to class Complex. We will need to do that ourselves and the knowledge we've gained in this section will be put to use again.

    In the next section, we will see how to overload = against other types of variables.


    <<Previous: Rules ^Up to Mayukh's World^ Next: More on Assignment >>

    Copyright © 2004 Mayukh Bose. All rights reserved.
    This work may be freely reproduced provided this copyright notice is preserved.
    OPTIONAL: Please consider linking to this website (http://www.mayukhbose.com/) as well.