Overloadable Operators

 < Day Day Up > 



Table 14.1 lists the overloadable operators. The right hand column gives an example prototype for one of the operators in the group for a user-defined type named Foo.

Table 14.1: Overloadable Operators

Operator Type

Operators

Example Prototype For Class Foo

Arithmetic
(Additive & Multiplicative)

+, -, *, /, %

Foo& operator+(Foo& lhs, Foo& rhs);

Assignment

=

Foo& operator=(Foo& rhs);

Compound Assignment

*=, /=, %=, +=, -=, >>=, <<=, &=, ^=, |=

Foo& operator+=(Foo& rhs);

Relational

<, >, <=. >=,

bool operator<(Foo& rhs);

Logical

!, &&, ||

bool operator!(Foo& rhs);

Equality

==, !=

bool operator==(Foo& rhs);

Shift

<<, >>

Foo& operator<<(int shift_by);

Bitwise

&, |, ^

Foo& operator&(int mask_value);

Comma

,

Foo& operator,(Foo& rhs);

Pointer-to-Member

->, ->*

Foo* operator->();

IOStream Insertion & Extraction

<<, >>

iostream& operator<<(iostream& out, Foo&);
iostream& operator>>(iostream& in, Foo&);

Increment & Decrement

++, --

Foo& operator++(); //postfix
Foo& operator++(int); //prefix

Foo& operator--(); //postfix
Foo& operator--(int); //prefix

Function Call

()

Foo& operator()(int val1, int val2);

Subscript

[]

Foo& operator[](unsigned int);

Unary

*, &, +,-, !, ~

Foo& operator+();

Allocation & Deallocation

new, new[]
delete, delete[]

Not covered in this chapter.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net