Operator overloading :: class member Vs namespace member

In C++, during operator overloading, what are the conditions that affect the function from becoming a class member Vs being declared at the namespace level.

Answer:

* When an overloaded operator is a class member, the class member operator is only invoked when the operator is used with a left operand that is an object of class type. If the operator must obe used with a left operand of another type, then the overloaded operator must be a namespace member.
* The =, [], () and member access arrow "->", are required by the language to be defined as class member operators. ... and definition of any of these at namespace level will be a compile time error.

Reference : "Operator Overloading" C++ Primer Lippmann p.744

No comments: