CSE2305 - Object-Oriented Software Engineering
Week 10

Topic 19: C++ Operator Overloading


Synopsis


Assigning to a class¤ object


Overloading operator=


Operator overloading¤ via member¤ functions


A slight problem of ordering


Operator overloading¤ via non-member¤ functions


How does the compiler know which operator function to use?


Friend¤ functions


Which operators can be overloaded?

  1. Unary operators: 
     
    +  -  *  &  ~  !  ++  --  ->  ->*
  2. Binary operators: 
     
    +  -  *  /  %  ^  &  |  <<  >> 
    += -= *= /= %= ^= &= |= <<= >>= 
    <  <=  >  >=  ==  !=  &&  || 
    ,  []  () 
    new  new[]  delete  delete[]


Which operators can't be overloaded?


Which operators should be overloaded?


An important use of operator overloading¤


Reading


This material is part of the CSE2305 - Object-Oriented Software Engineering course.
Copyright © Jon McCormack & Damian Conway, 1998–2005. All rights reserved.