Operator overloading in C ++
The following operators (marked with a space) can be overloaded as non-member functions:
new delete new[] delete[] + - * / % ˆ & | ˜ ! < > += -= *= /= %= ˆ=
&= |= << >> >>= <<= == != <= >= && || ++ -- , ->*
Below should be non-static member functions:
-> () [] =
The following should not be overloaded:
. .* :: ?: # ##
Conversion operators must also be member functions.
And just because it has '=' does not mean that it cannot be overloaded as a non-member operator. Well-formed:
struct A { };
A operator +=(A,A) { return A(); }
A a = A()+=A();
And the prefix and postfix increment and decrement operators can indeed be defined as non-member:
13.5.7 A custom function called operator ++ implements the prefix and postfix ++ operator. If this function is a member function without any parameters, or a non-member function with one class or enum type parameter, it defines the operator ++ prefix increment for objects of that type. If the function is a member function with one parameter (which must be of type int) or a non-member function with two parameters (the second of which must be of type int), it defines a postfix increment operator ++ for objects of that type. When the postfix increment is called as a result of using the ++ operator, the int argument will be null. Prefix and Postfix Reduction Operators - Processed similarly
The standard applies provision 13.5.
Hope it helps.
source to share
Operators that can be overloaded (comma is used as a separator):
+, -, *, /, %, ^, &, |, ~, !, =, <, >, +=, -=, *=, /=, %=, ^=, &=, |=, >>=, <<=,
!=, <=, >=, &&, ||, ++, --, ->* , (i.e., comma operator), ->, [], (), new[], delete[]
Operators that cannot be overloaded:
., .*, ::, ?:
Operators in which the overload function must be declared as a class method:
(), [], ->, any assignment operator
(as commenters pointed out)
source to share