为什么有些运营商喜欢::,。*,? :不能在C ++中重载? [英] Why some operators like :: , .* , ? : can not be overloaded in C++ ?

查看:127
本文介绍了为什么有些运营商喜欢::,。*,? :不能在C ++中重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I understood the concept of overloading , but I am not clear why some operators can not be overloaded .I did not get any clear answer of it.





我尝试过:



我理解重载的概念,但我不清楚为什么有些运算符不能重载。我没有得到任何明确的答案。



What I have tried:

I understood the concept of overloading , but I am not clear why some operators can not be overloaded .I did not get any clear answer of it.

推荐答案

::无法重载,因为它是范围限定符(或名称解析)运算符 - 它指定了一个类和命名空间名称,并且它在类级别不可用,因为它有助于指定你指的是哪个班级。



同样,。并且*不能重载,因为它们指定访问权限,而不是操作本身。 Dot指定一个类的成员,而star是一个指针解析运算符。



?:不能被覆盖,因为它不是真正的运算符:它的语法if ... then ... else语句返回一个值。
:: can't be overloaded because it is the scope qualifier (or name resolution) operator - it specifies a class and namespace name and it's not available at a class level because it helps to specify which class you are referring to.

Similarly, "." and "*" can't be overloaded because they specify access, not an operation per se. Dot specifies a member of a class, and star is a pointer resolution operator.

?: can't be overridden because it isn't really an operator: it's syntactic sugar for an if...then...else statement that returns a value.


作为一般答案,对于这些运算符中的some,重载并不是完全不可想象的,但是Stroustrup和C ++委员会没有看到对他们有意义的使用,同时可能引入含糊不清或意外的副作用。



(就个人而言,我认为这是一种耻辱只有三元运算符,'?:'不能超载 - 我相信我能找到它的用途;-))



C++编程/运营商/运营商重载 - 维基教科书,为开放世界打开书籍 [ ^ ]和 Stroustrup: C ++样式和技术常见问题解答:为什么我不能超载... [ ^ ]
As a general answer, for "some" of these operators, overloading isn't entirely unthinkable, but Stroustrup and the C++ committee didn't see a meaningful use for them, while potentially introducing ambiguities or unexpected side effects.

(Personally, I think it's a shame that the only ternary operator, '?:' can't be overloaded - I'm sure I could find a use for that ;-) )

See C++ Programming/Operators/Operator Overloading - Wikibooks, open books for an open world[^] and Stroustrup: C++ Style and Technique FAQ: Why can't I overload ...[^]


这篇关于为什么有些运营商喜欢::,。*,? :不能在C ++中重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆