20-CS-122-001 Computer Science II Spring 2012
Operator Overloading

Virtual functions, classes, inheritance, lists, queues, stacks, applications

Prev      Next      Lectures

The following may be overloaded:

Binary operators:
    /    *    +    -    %    ^    |    &    ~    !    <    >    >>   << 
    /=   *=   +=   -=   %=   ^=   |=   &=   ~=   !=   <=   >=   >>=  >>=
    &&   ||   ++   --   ,    new  delete

Unary operators:
    +    -    *    &

Overloaded only with member operator functions:
    =    [ ]   ( )   ->

The following operators cannot be overloaded:
    .    ?    :    ::    .*

Restrictions:

  1. At least one argument must be of user-defined type. Operators for built-in data types can not be changed (e.g. integers...)
  2. Operator functions can not have default values for arguments.
  3. The arity of an operator can not be changed.
  4. New operators cannot be introduced (e.g.: **)
  5. Operator precedence cannot be changed.