views:

261

answers:

2

Besides 'new', 'delete', '<<' & '>>' operators, what other operators can be overloaded in C++ outside of a class context?

+4  A: 

Operators that can be overloaded (comma used as delimiter):

+, -, *, /, %, ^, &, |, ~, !, =, <, >, +=, -=, *=, /=, %=, ^=, &=, |=, >>=, <<=, !=, <=, >=, &&, ||, ++, --, ->* , (i.e., comma operator), ->, [], (), new[], delete[]

Operators that can not be overloaded: ., .*, ::, ?:

Operators where overloading function must be declared as a class method: (), [], ->, any assignment operator (as the commenters noted)

bbg
Thanks for the quick response, but the question specifically asks for which of these can be overloaded outside of a class
Murali VP
@muralive: all of them?
peterchen
Not at all, for example, try overloading operator ++ outside a class.
Murali VP
The question asked for operators that aren't class methods. So anything with an `=` and the `->`, `[]`, and `()` operators don't belong.
eduffy
No. You can't override `->` outside of a class.
Edu Felipe
Don’t forget conversion operators (only inside classes, though)!
Konrad Rudolph
@muralive: overloading operator++ works just fine outside a class.
sepp2k
This should be down voted as it is the wrong answer.
Edu Felipe
+4  A: 

The following operators (delimitted by space) can be overloaded as non-member functions:

new delete new[] delete[] + - * / % ˆ & | ˜ ! < > += -= *= /= %= ˆ=  
&= |= << >> >>= <<= == != <= >= && || ++ -- , ->*

The following have to be non-static member functions:

-> () [] =

The following can not be overloaded:

. .* :: ?: # ##

conversion operators also have to be member functions.

And just because it has a '=' in it does not mean it cannot be overloaded as a non-member operator. The following is 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-members:

13.5.7 The user-defined function called operator++ implements the prefix and postfix ++ operator. If this function is a member function with no parameters, or a non-member function with one parameter of class or enumeration type, it defines the prefix increment operator ++ for objects of that type. If the function is a member function with one parameter (which shall be of type int) or a non-member function with two parameters (the second of which shall be of type int), it defines the 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 have value zero.
The prefix and postfix decrement operators -- are handled analogously

Clause 13.5 in the Standard covers this.

Hope this helps.

Faisal Vali
sbk
@sbk Thanks for the feedback - the fragment was not supposed to be an example of "good" operator overloading style - but was intended to indicate that overloading such operators (@=) as non-member functions is valid - and you're wrong about conforming compilers not accepting the fragment as well formed code - it is syntactically valid - semantically it is a bad idea - but not only does the standard say little about what the semantics of overloaded operators should be - semantics and good style were not the focus of my answer nor the OP's question ;)
Faisal Vali