3

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

1
  • 1
    Outside of class context meaning, not inside a class but still involves a class object in arguments or a class return type Commented Sep 19, 2009 at 14:04

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Nitpicking: Operator +=, similar to operator = and pretty much anything with = in it, should return an lvalue. I'd rewrite your operator definition like this: A& operator += (A&, A) { ... }. Your other line won't compile then, but A() += A() is a little bit counter-intuitive, applying += on temporary is useless (not to mention that conforming C++ compiler won't accept it)
@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 ;)
4

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)

6 Comments

Thanks for the quick response, but the question specifically asks for which of these can be overloaded outside of a class
Not at all, for example, try overloading operator ++ outside a class.
The question asked for operators that aren't class methods. So anything with an = and the ->, [], and () operators don't belong.
No. You can't override -> outside of a class.
Don’t forget conversion operators (only inside classes, though)!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.