operator overloading : unary operators

use the following rules of thumb to decide:
If it is a unary operator, implement it as a member function.

Why can't we make global friend / non-friend functions functions?

They do not modify the private data (in case of ! , + , -)
They do modify the private data (in case of ++ prefix/postfix).

Q1 : Why is a rule of thumb to make them member function when they don't modify data.

Q2 : What if there is no class? I invoke +2 as int is not a class.

Last edited on
Where does this advice come from?

The general recommendation that I am familiar with is that as many operators as possible should be non-member friend functions so as to allow for implicit conversions.

Q2: C++ requires at least one type (excluding return type) in an operator overload to be a non-primitive type. You cannot change the behavior of the built in types.
Hm, I don't know the answer. The long conversation in the comments doesn't seem to touch on it either.
Topic archived. No new replies allowed.