为什么不能在 C++ 中重载所有内容?

发布于 2024-10-21 23:49:09 字数 129 浏览 2 评论 0原文

sizeoftypeid 不能重载是合理的,但我看不出重载 ?:, .* 有何危害.。这有技术原因吗?

It is reasonable that sizeof and typeid cannot be overloaded, but I can't see the harm in overloading ?:, .* and .. Are there technical reasons for this?

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

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

发布评论

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

评论(6

紙鸢 2024-10-28 23:49:09

引用 Bjarne Stroustrup 的话:

没有根本原因
禁止重载 ?:。我只是
没觉得有必要介绍
重载三元的特殊情况
操作员。注意一个函数
重载 expr1?expr2:expr3 会
无法保证只有一个
expr2 和 expr3 被执行。

...

操作员。 (点)原则上可以是
使用与以下相同的技术进行重载
用于->。然而,这样做可以
引发关于是否
操作是针对对象的
重载 . 或引用的对象
通过 . ...这个问题可以解决
有几种方法。当时
标准化,不明显
哪种方式最好。

来源

To quote Bjarne Stroustrup:

There is no fundamental reason to
disallow overloading of ?:. I just
didn't see the need to introduce the
special case of overloading a ternary
operator. Note that a function
overloading expr1?expr2:expr3 would
not be able to guarantee that only one
of expr2 and expr3 was executed.

...

Operator . (dot) could in principle be
overloaded using the same technique as
used for ->. However, doing so can
lead to questions about whether an
operation is meant for the object
overloading . or an object referred to
by . ... This problem can be solved in
several ways. At the time of
standardization, it was not obvious
which way would be best.

Source

梦断已成空 2024-10-28 23:49:09

如果重载 .,您将如何访问类成员? obj.data 的含义是什么?

If you overload ., how would you access class members? What would be the meaning of obj.data?

梦归所梦 2024-10-28 23:49:09

语法是什么?

事实上,不重载任何运算符是有充分理由的
它不会评估它的所有操作数:你不应该
过载&&或||任一(特殊情况除外)。你不能
使用重载运算符来模拟这一点。考虑一些事情
喜欢:
p!= 空?默认值:p->getValue()
其中 defaultValue 或 p->getValue() 的类型导致重载
决心承受你的超负荷。这是一个常见的习语,但是
如果你超载,它就无法工作?:。

What would the syntax be?

In fact, there are good reasons for not overloading any operator
which doesn't evaluate all of its operands: you shouldn't
overload && or || either (except in special cases). You can't
simulate this with an overloaded operator. Consider something
like:
p != NULL ? defaultValue : p->getValue()
where the type of defaultValue or p->getValue() causes overload
resolution to pick up your overload. It's a common idiom, but
it can't be made to work if you overloaded ?:.

拒绝两难 2024-10-28 23:49:09

这里有一些阅读材料C++ FAQ Lite :)

一般来说,重载上面的运算符没有任何好处。您将尝试实现哪些附加语义?

重载运算符的原因是为类的用户提供直观的语法。例如,对字符串重载 + 和 += 是有意义的。对于其他开发人员来说,这意味着什么是显而易见的。

您要重载什么确实不明显?: for ... 也就是说,据我所知,没有任何技术原因阻止这些运算符重载。

重载->运算符允许创建引用计数指针,例如 boost::shared_ptr。 “否定”对象的概念在不同的上下文中可能有不同的含义,因此偶尔重载此运算符是合理的。

Here's some reading material C++ FAQ Lite :)

In general there would be no benefit to overloading the operators above. What additional semantics would you be trying to implement?

The reason for overloading operators is to provide intuitive syntax to the user of your class. For example, it makes sense to overload + and += for strings. It's obvious to another developer what that means.

It's really not obvious what you would overload ?: for ... That said there are no technical reasons I am aware of that prevented these operators from being overloaded.

Overloading the -> operator allows for reference counted pointers to be created such as boost::shared_ptr. The concept of 'negating' an object might have different meanings in different contexts, so it's reasonable to occasionally overload this operator.

好多鱼好多余 2024-10-28 23:49:09

定义“operator bool”就足以让 ?: 工作。
对于运营商 .想想这个:SomeClass."SomeString!!"

这些重载会阻止编译器的词法分析器正确解析文件。

Defining "operator bool" is enough for ?: to work.
For operator . think of this: SomeClass."SomeString!!"

These overloadings prohibit compiler's lexer from parsing the file correctly.

酷到爆炸 2024-10-28 23:49:09

可以重载大多数运算符的原因是能够模拟内置类型。由于所有内置类型都不能使用 . 运算符,因此它没有任何作用。 operator*operator-> 在那里,您可以创建自己的指针类。所有数学和布尔运算符都可以创建您自己的数字类。

The reason you can overload most operators is to be able to simulate built in types. Since none of the built in types can use the . operator, it wouldn't serve any purpose. operator* and operator-> are there so you can make your own pointer classes. All the math and boolean operators are there to be able to make your own numeric classes.

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