为什么不能在 C++ 中重载所有内容?
sizeof
和 typeid
不能重载是合理的,但我看不出重载 ?:
, .* 有何危害
和 .
。这有技术原因吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
引用 Bjarne Stroustrup 的话:
...
来源
To quote Bjarne Stroustrup:
...
Source
如果重载
.
,您将如何访问类成员?obj.data
的含义是什么?If you overload
.
, how would you access class members? What would be the meaning ofobj.data
?语法是什么?
事实上,不重载任何运算符是有充分理由的
它不会评估它的所有操作数:你不应该
过载&&或||任一(特殊情况除外)。你不能
使用重载运算符来模拟这一点。考虑一些事情
喜欢:
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 ?:.
这里有一些阅读材料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.
定义“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.
可以重载大多数运算符的原因是能够模拟内置类型。由于所有内置类型都不能使用
.
运算符,因此它没有任何作用。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*
andoperator->
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.