Operator overloading outside class [duplicate]

The basic question is “Do you want conversions to be performed on the left-hand side parameter of an operator?”. If yes, use a free function. If no, use a class member.

For example, for operator+() for strings, we want conversions to be performed so we can say things like:

string a = "bar";
string b = "foo" + a;

where a conversion is performed to turn the char * "foo" into an std::string. So, we make operator+() for strings into a free function.

Leave a Comment