Programming and Graphics

(Kiana) #1

6.9 Operator overloading 171


+ - * / = < > += <<= >>= ==


!= <= >= ++ -- ~ &= ^= |= && ||


%= [] -= *= /= << >> delete % & ^
! | new[] () , ->* -> new delete[]

Table 6.9.1Operators that can be overloaded in C++. The right-to-left copy
assignment operator (=) is overloaded by default.


Statement Operator Syntax


  • a +- &! ̃++-- A::operator•()
    a• ++ -- A::operator•(int)
    a•b +-
    /%^ operator•(A, B)
    a•b &|<>== != operator•(A, B)
    a•b <=>=<< >>&&||, A::operator•(B)
    a•b =+=-=*=/= A::operator•(B)
    a•b %=^=&= A::operator•(B)
    a•b |=<<=>>=[] A::operator•(B)
    a(b, c...) () A::operator() (B, C...)
    a->x -> A::operator->()


Table 6.9.2Syntax of common operators overloaded as class functions.•stands
for an operator, a is a member of class A, b is a member of class B, and c is
amemberofclassC.


Statement Operator Syntax


  • a +- &! ̃++-- operator•(A)
    a• ++ -- operator•(A,int)
    a•b +-
    /%^ operator•(A, B)
    a•b & | <>== != operator•(A, B)
    a•b <= >= << >> && || , operator•(A, B)


Table 6.9.3Syntax of common operators overloaded as global functions.•stands
for an operator, a is a member of class A, b is a member of class B, and c is
amemberofclassC.


is implemented by member functions, and global overloading is implemented by
outside functions.


As an example, we overload the ++ operator in the algebra class by
inserting the following declaration in the public section of the class:

Free download pdf