#Overloading operators inside classes and outside?

5 messages · Page 1 of 1 (latest)

mighty magnet
#

I noticed when going through learn Cpp that there are two ways to overload operators you can do it inside the class or outside a class you can also do it using friend functions. I’m just unsure of what the difference is. I read that doing it inside the class essentially gives it an implicit this pointer so you’d only need to give it one argument but if you do it outside the class you’d need two arguments. Is there any benefit of doing it with a friend function if you can just do it in the class already?

night owlBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

ionic sierra
#

Some operators are forced to be members, like =, ()/[], maybe something else

#

Making an operator a member introduces asymmetry, e.g. I think the this parameter can't do implicit conversions. Therefore arithmetic operators like +,-,*, whatever, usually work better as non-members

#

And if you decided that a specific operator shouldn't be a member, then it's better to make it a friend definition than a free function, because those are only callable via ADL, so the compiler has less overloads to search through