不可重载的 C++运营商

发布于 2024-09-02 15:34:08 字数 22 浏览 5 评论 0原文

C++ 中哪些运算符不能重载?

What operators can not be overloaded in C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一个人的夜不怕黑 2024-09-09 15:34:08

我很确定 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 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.

风苍溪 2024-09-09 15:34:08

来自维基百科:

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)

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)

葬花如无物 2024-09-09 15:34:08

来自这篇关于运算符重载的文章

大多数都可以超载。唯一不能的 C 运算符是 .和 ?: (以及 sizeof,从技术上讲,它是一个运算符)。 C++ 添加了一些自己的运算符,除了 :: 和 .* 之外,大多数运算符都可以重载。

所以

  • .
  • ?:
  • ::
  • .*

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

  • .
  • ?:
  • ::
  • .*
浅唱ヾ落雨殇 2024-09-09 15:34:08

..*?:::sizeof 和 <代码>类型ID。 (来自 http://en.wikipedia.org/wiki/C%2B% 2B_operators#Other_operators)

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

撑一把青伞 2024-09-09 15:34:08

以下运算符不能在 C++ 中重载:

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

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)
盗琴音 2024-09-09 15:34:08

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

大多数都可以超载。唯一的C
不能的运算符是 .和 ?:
(和 sizeof,从技术上讲,这是一个
操作员)。 C++添加了一些自己的
运算符,其中大部分可以是
重载,除了 :: 和 .*。

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 .*.

冷︶言冷语的世界 2024-09-09 15:34:08

.:?::.*typeid 和 <代码>sizeof 运算符。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文