I needed to overload the comparison operator of a Profile
class such that I could sort the profiles by usernames.
After readings this post it seems the most common way to implement the comparison operator is as a member function:
bool Profile::operator<(const Profile &p2)
{
return username < p2.get_username();
}
The way I tried implementing it before looking at the web was using a helper function:
bool operator<(const Profile &p1, const Profile &p2)
{
return p1.get_username() < p2.get_username();
}
Trying to figure out if there was any difference I stumbled upon questions like this. But it seems that there is only a difference in helper vs member implementation if the overloaded operator modifies the object (unless I have misunderstood something).
Is this the case?
1