* Ian Collins:
Jerry Fleming wrote:
>As I am newbie to C++, I am confused by the overloading issues. Everyone
says that the four operators can only be overloaded with class member
functions instead of global (friend) functions: (), [], ->, =. I wonder
why there is such a restriction.
If there wasn't, how could you specify the left hand side of an expression?
In the same way as with other operators: as an argument.
E.g., if it weren't for the language restriction,
MyClass& operator=( MyClass& lhs, MyClass const& rhs ) { ... }
I don't know why that isn't allowed, but one common feature is that
these operators are not meaningful for enum types, only for class types.
All unary operators have to be class members.
Sorry, that's incorrect.
§13.5.1 "A prefix unary operator shall be implemented by a non-static
member function (9.3) with no parameters or a non-member function with
one parameter. Thus, for any prefix unary operator @, @x can be
interpreted as either x.operator@ or operator@(x)."
And ditto for postfix ones.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?