What operators can not be overloaded in C++?
-
7i think a google search will give you a solution. by the way what exactly is the reason for u asking this question. – ckv May 20 '10 at 14:25
-
2Indeed, google (and wikipedia) searches provide answers to many questions asked here, but so what? If everyone always used some other search engine before coming here, then that would spend the eventual doom of this site. – Christopher Barber May 20 '10 at 14:29
-
@viswanathan - Sorry, I thought it was an interesting question. Will spend more time on a search engine next time rather than posting here. – MKaufmann May 20 '10 at 14:55
-
@christopher but is'nt it better to see previous thread or use google to find answer to the obvious question?but that said ,that would have been stand true if question would have been like "which operator can be overladable in c++".But extra not in the question has made it relevant here. – mawia May 20 '10 at 15:48
-
2It obviously would be better to search for answers on this site before posting a question, and there is no question that google often points you at better answers than you can get here. I was just pointing out that if one were to always go to google first then fewer questions will get asked and answered here and this site will eventually become irrelevant. – Christopher Barber May 20 '10 at 16:38
7 Answers
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.

- 476,176
- 80
- 629
- 1,111
-
+1. You are missing `?:` in the first paragraph, but the rest made me upvote you. – Gorpik May 20 '10 at 14:34
-
1@Gorpik:`?:` is the ternary operator, so it's included, just under a different name... – Jerry Coffin May 20 '10 at 14:42
-
@Jerry Coffin: Oops, I am afraid I just skimmed over the first paragraph and only saw the operators themselves. – Gorpik May 20 '10 at 15:06
-
@Matthieu: If I'm not mistaken, it overloads the *binary* `&`, not the unary one. OTOH, Boost::Assign does overload the comma operator, and makes it work pretty reasonably too (for that matter, I've done the same, though not as nicely). I can't remember for sure, but Boost::Spirit probably overloads a couple I recommended against as well. – Jerry Coffin May 20 '10 at 15:25
-
"The && and || also only (normally) evaluate the right operand if necessary." can anyone explain this line in more detail? thanks! – mawia May 20 '10 at 15:59
-
@Jerry: right, my mistake, it is the binary &. I have tendency to be more lax for DSEL so I won't blame Spirit :) As for Assign, I've always found that the `operator()()` could make as good a job as the comma with some proxy object in the mix. – Matthieu M. May 20 '10 at 17:20
-
@mawia: these two operators (when working on boolean values) will first evaluate the first term, and only evaluate the second (ie invoke the function) if the first is not sufficient. A classic example is `Object* p = /**/; if (p && p->isValid())`. If `&&` was evaluating the 2 terms (left and right), then we would be using a null pointer for calling `isValid` which would lead to Undefined Behavior. However, if you overload this operator, then both terms will be evaluated when your overloaded operator is selected, thus changing the semantics it traditionnally carries. – Matthieu M. May 20 '10 at 17:23
-
@Matthieu M you can't overload operators applied to pointers, only those of user defined types, so your justification for && would only apply to smart pointers. – Pete Kirkham Jun 21 '10 at 08:13
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
.
?:
::
.*

- 75,627
- 68
- 187
- 294
-
As Fred Larson points out, if you want to be picky sizeof and typeid are operators as well. – BCS May 20 '10 at 14:27
From Wikipedia:
Operator Name Syntax
Bind pointer to member by reference a.*b
Member a.b
Scope resolution a::b
Size of sizeof(a)
Ternary a ? b : c
Type identification typeid(a)

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

- 60,987
- 18
- 112
- 174
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)

- 94,250
- 39
- 176
- 234
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 .*.

- 74,820
- 18
- 121
- 166
-
-
2I 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 May 20 '10 at 14:27