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

    Unary Operators

    The following are a list of unary operators that can be overloaded in C++
    • Unary minus (-)
    • Unary plus (+)
    • Ones complement (~)
    • Address of (&)
    • Pointer dereference (*)
    • Logical NOT (!)
    Of these operators, the most familiar to us are the unary minus and pointer operators.
        x = -y; // Unary minus.
        x = &y; // Address of operator.
        x = *y; // Pointer dereference.
        x = +y; // Unary plus is not very common. It is a valid C++ operator though.
        
    Of course, ++ and -- are also unary operators, but they've been covered separately under the Increment/Decrement section already. We will study how to overload the unary minus operator (both as a member function and a global function) in detail. We start by implementing unary minus as an overloaded member function.
    	  class Complex {
    	  private:
    	    double real, imag;
    	  public:
    	    Complex() { real = imag = 0; }	
    	    ....
    	    Complex operator-(void); 
    	  };
    	  
    	  Complex Complex::operator-(void) {
    	    Complex tmp;
    	    tmp.real = -real;
    	    tmp.imag = -imag;
    	    return tmp;
    	  }
    
    	  int main(void) {
    	    Complex num1(2, 3);
    	    Complex num2;
    	    num2 = -num1;
    	    cout << "num2 is " << num2.GetReal() << " + "
    	         << num2.GetImag() << "i" << endl;
    	  }
        
    Now, we can do the same thing using a global function.
    	  class Complex {
    	  private:
    	    double real, imag;
    	  public:
    	    Complex() { real = imag = 0; }	
    	    ....
    	    // Comment out --- Complex operator-(void); 
    	  };
    	  
    	  Complex operator-(const Complex& num) {
    	    Complex tmp;
    	    tmp.SetReal(-num.GetReal());
    	    tmp.SetImag(-num.SetImag());
    	    return tmp;
    	  }
        
    You can verify that both implementations work the same way. You can implement unary plus (+) similarly. All the unary operators may be overloaded using the following patterns:
           returntype operator unaryop(void); // if using a class member function
           returntype operator unaryop(const ClassName& arg); // if using a global function.
        
    where unaryop is an unary operator from the list at the top of this section, and returntype is the returned type of variable. Note that for some unary operators, it may make sense to return a different type. For instance, the ! overloading function may return a bool or an int type (or any other type that makes sense). Similarly, the overloaded address-of (&) operator may return an unsigned int or an unsigned long.
        // implemented as global function.
        bool operator!(const Complex& num) {
          if (num.GetReal() == 0 && num.GetImag() == 0)
            return true;
          else
            return false;
        }
        
    In the above example, we overloaded the logical NOT (!) operator to return bool instead of Complex, since it makes more sense for this operator.


    <<Previous: Operator-Assignment Overloading ^Up to Mayukh's World^ Next: Relational Operators >>

    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.