The operators are = () [] -> ->* conversion operators
These can be declared only as member functions.
Any other operator function can be either a class member or a non-member function.
What is the rationale for this restriction?
The operators are = () [] -> ->* conversion operators
These can be declared only as member functions.
Any other operator function can be either a class member or a non-member function.
What is the rationale for this restriction?
The rationale is that it would not make sense for them to be non-members, as the thing on the left-hand side of the operator must be a class instance.
For example, assuming a class A
A a1;
..
a1 = 42;
The last statement is really a call like this:
a1.operator=(42);
It would not make sense for the thing on the LHS of the . not to be an instance of A, and so the function must be a member.
Because you can't modify the semantics of primitive types. It wouldn't make sense to define how operator=
works on an int
, how to deference a pointer, or how an array access works.