views:

283

answers:

7

What operators can not be overloaded in C++?

+2  A: 

GIYF: http://www.google.com/search?q=What+operators+can+not+be+overloaded+in+c%2B%2B%3F

First result:

http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.5

Most can be overloaded. The only C operators that can't be are . and ?: (and sizeof, which is technically an operator). C++ adds a few of its own operators, most of which can be overloaded except :: and .*.

Tyler McHenry
I found that via "operator overloading c++ list" what did you use?
BCS
I knew it was in the C++ FAQ Lite, but I just copy-pasted the original question into Google, and lo and behold, first result!
Tyler McHenry
Maybe the auto search should include Google results as well...
BCS
+9  A: 

From this article on operator overloading

Most can be overloaded. The only C operators that can't be are . and ?: (and sizeof, which is technically an operator). C++ adds a few of its own operators, most of which can be overloaded except :: and .*.

so

  • .
  • ?:
  • ::
  • .*
BCS
As Fred Larson points out, if you want to be picky sizeof and typeid are operators as well.
BCS
+3  A: 

., .*, ?:, ::, sizeof, and typeid. (from http://en.wikipedia.org/wiki/C%2B%2B_operators#Other_operators)

Fred Larson
+2  A: 

The ., :?, ::, .*, typeid and sizeof operators.

Jacob
+3  A: 

The following operators can't be overloaded in C++:

.                       example: object.member_function()
.*                      example: object_reference.*member_function_ptr();
::                      example: some_name_space::function()
?:                      example: z = y > z ? y : z     (ternary operator)
AraK
+17  A: 

I'm pretty sure the C++ FAQ Lite probably covers this. The ones I can think of right off are the ternary operator, the . operator and the scope resolution operator (::). Thinking a moment, since the . operator can't be overloaded, .* probably can't be either.

There are also some operators that can but almost never should be overloaded, including the comma operator, &&, ||, all of which normally create a sequence point. The && and || also only (normally) evaluate the right operand if necessary. Neither of those characteristics is true of an overloaded operator.

While there are a few reasons to do so, overloading the unary & (address-of) operator is also often a pretty poor idea. The address of an object is largely equated with its identity, so overloading it can make quite a few other things relatively difficult.

Edit: as far as evaluating the right operand only if necessary (aka "Short circuit evaluation"): consider something like x && y. The expression can only be true if if the left operand is true. If the left operand evaluates to false, then the expression must also be false, and C (and C++) guarantee that the right operand will not be evaluated at all. This is convenient (for example) if you want to do something like if (ptr != NULL && ptr->member /*...*/ ). In this case, if the pointer in NULL, execution stops, and you never attempt to dereference the pointer.

The same basic idea is true with ||, but in reverse. In this case, if the left operand evaluates to true, then the expression as a whole must be true, regardless of what the right operand would evaluate to so (again) C and C++ guarantee that in this case the right operand won't be evaluated.

When you overload those operators, however, evaluating the expression all will always evaluate both operands. The first expression would attempt to dereference the pointer even if it is a null pointer, so it would give undefined behavior.

Jerry Coffin
very good stuff in the second paragraph.
BCS
+1. You are missing `?:` in the first paragraph, but the rest made me upvote you.
Gorpik
@Gorpik:`?:` is the ternary operator, so it's included, just under a different name...
Jerry Coffin
@Jerry Coffin: Oops, I am afraid I just skimmed over the first paragraph and only saw the operators themselves.
Gorpik
Jerry Coffin
mawia
Matthieu M.
Matthieu M.
Pete Kirkham
+8  A: 
Jaap Weidemann