Wednesday, October 20, 2004
Operator Overloading

Topics

Operator Overloading
- What you think of as "operators", in other words  +,  -,  =,  >, [ ], etc., are actually functions, called operator functions.
- When you have an operator such as   +   overloaded in C++, and you have two objects of a class

SomeClassName x, y;

and x and y are initialized to some appropriate values, then you can write

x + y

in your C++ code.

- The compiler changes commutative binary operators that don't change the values, such as  x + y , into a global function: operator+(x,y).
- The compiler changes operators that change the value of an object, such as unary operators and assignment, into member functions.  For instance,
x += y becomes x.operator+=(y).
- This is only a syntactical convenience for you as a programmer.  Overloaded operators are not necessary for a language, and some language theorists think they just add unnecessary complexity.  For instance, Java decided not to include overloaded operators, but C# decided to include them.
- You can overload operators with unlike datatypes.  For instance, you could overload 
SomeClass& operator+(SomeClass& left, int right) to add an integer to an object of SomeClass.
- You are also not restricted to any particular return data type.  For instance, in the example just above, you could choose to return an
int instead of SomeClass& .

 

The operator functions you can overload
- Functions that can be used to extend the built-in set of C++ operators

  • mathematical operators (+, -, *, /, %, ++, --, etc...)
        operator+, operator-, operator*, etc...
  • relational operators (<, >, ==, etc...)
        operator<, operator>, operator==, etc...
  • stream operators(<<, >>)
        operator<<, operator>>
       
  • the assignment operators (=, +=, -=, *=, etc...)
        operator=, operator+=, operator-=, etc...
  • logical operators (&&, ||, !, etc...) (usually valid only for boolean types on each side, so these rarely have appropriate meaning for objects on both sides)
           
    operator&&, operator||, operator!, etc...
  • dereference operators ([], *, ->), but be careful because they can get rather technical.
        
     
    operator[], operator*, operator->

  • - With the intrinsic data types (float, char, ...),  these operators have unchangeable meaning.  You can only overload operators on the classes you create.
    - Each operator can be given a new meaning for your classes simply by writing a function that implements that meaning, but it is strongly suggested that you keep the meaning of the function close to what programmers would normally expect for that function.  Don't make the   +  operator act like a minus.
    - If the operator is normally commutative in mathematics, such as + is, then make your definition so that it does not matter which object is on which side. 
    - If the operator is not commutative, make your definition non-commutative also, in a manner that is logical for that operator.
    - Note that the equals operator, =, is commutative in mathematics, but the assignment operator, =, in programming is not.  The equality operator in C, ==, is the equivalent of the equals operator in mathematics, and it should be commutative.

    Member and non-member functions


    - Your overloaded operators can be defined as member or non-member functions of your class.
    - Member functions use the object on the left of the operator to call the function, and pass the object on the right as a parameter.  Thus 
    x += y   becomes x.operator+=(y), the overloaded operator += function for that class. Normally member functions are used for operators that change the value of the object, such as assignment or unary operators. 
    - Non-member functions are global functions, and are usually defined as friends of the class.   They send both objects as parameters to the global function.  Thus 
    x + y   becomes operator+(x, y), the global operator+ function with two parameters.  Usually global functions are used for operators that do not change the value of either parameter object.  Also, since they are global functions, they can be defined as commutative binary functions, where the order of the two parameters does not matter.

    Standard functions and code for overloaded operator functions.

    - Since overloaded functions can be very tricky, I have supplied "cookie cutter" code to create a class with almost all the possible overloaded functions.
    - Follow these hyperlinks for the standard code:
        StandardFunctions.h and StandardFunctions.C
    - Do not stray far from this code, or your overloaded operators may not work as expected.
    - Except for substituting your class name for MyClass, keep all non-comment code exactly as is.  Add your code only where it says:
    // appropriate code goes here to ...
     

    Readings

    Deitel & Deitel: 8.1 - 8.7, skip 8.5

     

    Back to Csc 125 Programming in C++
      Scott Badman  Office:  B132  Phone:  353-2250  sbadman@parkland.edu  

    Parkland College, 2400 W. Bradley Avenue, Champaign, IL 61821