>>a + b would be a.operator+(b) but then how can 5+b be 5.operator+(b) since 5 is a int.
You're wrong. Although you can make an overloaded operator as a member function (i.e. a.operator+(b)), that's not the preferred way, normally it is implemented as a free-function. As so:
// this will cover the ( a + b ) case:
Element operator + (const Element& a, const Element& b);
// this will cover the ( 5 + b ) case:
Element operator + (int a, const Element& b);
// this will cover the ( a + 5 ) case:
Element operator + (const Element& a, int b);
Overloaded operators should almost always be implemented as (friend) free-functions, not as member functions. The only exception to that rule is for the operators that cannot be implemented as free-functions, like assignment operators and a few others.
mike_2000_17
Posting Virtuoso
Moderator
2,139 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457