不可重载的 C++运营商
C++ 中哪些运算符不能重载?
What operators can not be overloaded in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
C++ 中哪些运算符不能重载?
What operators can not be overloaded in C++?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
我很确定 C++ FAQ Lite 可能涵盖了这一点。我立即能想到的是三元运算符、
.
运算符和作用域解析运算符 (::
)。想了一下,由于.
运算符不能重载,因此.*
可能也不能重载。还有一些运算符可以但几乎不应该重载,包括逗号运算符、
&&
、||
,所有这些通常都会创建序列点。&&
和||
也仅(通常)在必要时评估正确的操作数。对于重载运算符来说,这两个特征都不成立。虽然这样做有几个原因,但重载一元
&
(地址)运算符通常也是一个非常糟糕的主意。对象的地址在很大程度上等同于它的身份,因此重载它可能会使许多其他事情变得相对困难。编辑:至于仅在必要时评估正确的操作数(又名“短路评估”):考虑类似
x && y 。仅当左操作数为 true 时,表达式才为 true。如果左操作数的计算结果为 false,则表达式也必须为 false,并且 C(和 C++)保证右操作数根本不会被计算。例如,如果您想要执行类似
if (ptr != NULL && ptr->member /*...*/ )
的操作,这会很方便。在这种情况下,如果指针为 NULL,执行就会停止,并且您永远不会尝试取消引用该指针。||
的基本思想也是如此,但方向相反。在这种情况下,如果左操作数的计算结果为true
,则表达式作为一个整体必须为true
,无论右操作数的计算结果为何(再次) C C++ 保证在这种情况下不会评估正确的操作数。然而,当您重载这些运算符时,计算表达式 all 将始终计算两个操作数。第一个表达式将尝试取消引用指针,即使它是空指针,因此它会给出未定义的行为。
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 tofalse
, 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 likeif (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 totrue
, then the expression as a whole must betrue
, 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.
来自维基百科:
From Wikipedia:
来自这篇关于运算符重载的文章
所以
.
?:
::
.*
From this article on operator overloading
so
.
?:
::
.*
.
、.*
、?:
、::
、sizeof
和 <代码>类型ID。 (来自 http://en.wikipedia.org/wiki/C%2B% 2B_operators#Other_operators).
,.*
,?:
,::
,sizeof
, andtypeid
. (from http://en.wikipedia.org/wiki/C%2B%2B_operators#Other_operators)以下运算符不能在 C++ 中重载:
The following operators can't be overloaded in C++:
GIYF: http://www.google.com/search?q=What+operators+can+not+be+overloaded+in+c%2B%2B%3F
第一个结果:
http://www.parashift.com/c++-faq-lite/operator-overloading。 html#faq-13.5
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
.
、:?
、::
、.*
、typeid
和 <代码>sizeof 运算符。The
.
,:?
,::
,.*
,typeid
andsizeof
operators.