- Operator functions
- Member functions
- Use this keyword to implicitly get argument
- Gets left operand for binary operators (like +)
- Leftmost object must be of same class as operator
- Non member functions
- Need parameters for both operands
- Can have object of different class than operator
- Must be a friend to access private or protected data
- Called when
- Left operand of binary operator of same class
- Single operand of unitary operator of same class
- Overloaded operator
- Left operand of type ostream &; Such as cout object in cout classObject
- Similarly, overloaded needs istream &
- Thus, both must be non-member functions
- Commutative operators
- May want + to be commutative; So both "a + b" and "b + a" work
- Suppose we have two different classes
- Overloaded operator can only be member function when its class is on left
- HugeIntClass + Long int
- Can be member function
- When other way, need a non-member overload function; Long int + HugeIntClass
2004-11-01