C++运算符重载
在C#中,重载任何运算符的方法(函数)必须是static
和public
。
我认为将其设为静态是一件好事,每个对象不需要它自己的版本。
但在 C++ 中,它不必是静态
。为什么 C# 强制执行这一点而 C++ 不强制执行?
两种设计的优点和缺点是什么?
In C#, the method(function) of overloading any operator must be static
and public
.
I see that making it static
is a good thing, every object doesn't need its own version of it.
But in C++, it doesn't have to be static
. Why does C# enforce this and C++ doesn't?
What are the advantages and disadvantages of both designs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么 C++ 不强制运算符重载为“静态”?
如果您将重载运算符函数设为静态,它将无法访问
this
。人们需要访问重载函数内的this
,因为该函数通常会更改this
的状态。如果您不需要访问该函数内的
this
,则可以创建一个重载运算符函数static
,这实质上意味着您不会操纵该函数所在对象的状态。运算符函数被调用。所以这是可能的,但不是通常的或本质上期望的。将全局重载运算符函数设置为静态会将运算符函数的范围限制为同一文件。
鉴于上述两者,编译器不会强制重载运算符函数为静态,因为强制它不会提供真正的优势或便利,而不是强制相同提供更多便利。
为什么 C# 强制运算符重载为“静态”?
这比我能更好地解释它。
优点和缺点是什么?
好吧,第一个问题的答案确实说明了何时可以将重载运算符函数设置为 static & 。这解释了优点/缺点。
why C++ Doesn't enforce operator overloading to be "Static" ?
If you make overloaded operator function static it wont have access to
this
. One would need access tothis
inside the overloaded function, as usually the function would change the state ofthis
.You can make a overloaded operator function
static
if you don't need access tothis
inside that function, which essentially means you are not manipulating the state of the object on which the operator function was invoked. So it is opssible but not usual or essentially desired.Making an globally overloaded operator function static would limit the scope of the operator function to the same file.
Given the both above, Compiler doesn't enforce overloaded operator functions to be static since enforcing it would provide no real advantage or convenience, rather not enforcing the same provides more convenience.
why C# enforces operator overloading to be "Static" ?
This explains it much better than I can.
What are the advantages and disadvantages?
Well, the answer to the first Question does say out when one could make a overloaded operator function static & that explains the advantage/disdvantage.
我不是 C# 设计师之一。一个潜在的原因是,C++ 的经验表明,运算符参数之间的不对称(这是拥有成员二元运算符的直接结果)是一个坏主意。
因为没有以前的经验可以借鉴,而且这似乎是个好主意? (这种区别在 Algol68 中没有意义,Algol68 是早于 C++ 的少数具有运算符重载的语言之一。顺便说一句,C++ 在运算符重载方面没有重复 Algol68 的错误。)
I'm not one of the C# designer. A potential reason is that experience with C++ showed that having an asymmetry between the arguments of operators (which is a direct consequence of having member diadic operators) is a bad idea.
Because there was no previous experience to build on and it seemed a good idea? (The distinction doesn't make sense in Algol68, one of the few language predating C++ which had operator overloading. BTW C++ didn't repeat Algol68 mistakes in operator overloading.)
因为它们是不同的语言。我不太懂 C#,所以我不会
对它的评论太多了,但是在 C++ 中,一些运算符,例如
[]
和()
(更不用说复制赋值运算符)不能是静态的,对于
各种原因。一般来说,为什么你想要一个操作员
静止的?我认为我从未在 C++ 中将运算符设为静态。这
经典的二元算术运算符通常是自由函数,并且
所有其他运营商成员。 (有人可以争论是否更好
对于
operator++
是成员函数或自由函数,但最宽传播实践似乎是让他们成为会员。)
Because they're different languages. I don't really know C#, so I can't
comment on it too much, but in C++, some operators, like
[]
and()
(not to mention the copy assignment operator) can't be static, for
various reasons. And in general, why would you want an operator to be
static? I don't think I've ever made an operator static in C++. The
classical binary arithmetic operators are usually free functions, and
all of the other operators members. (One can argue whether it is better
for
operator++
to be a member or a free function, but the most widespread practice seems to be to make them members.)
我认为 C# 强制执行它是因为它更有意义,而不是因为它是错误的。
添加 2 个对象时,我很难理解
this
参数,所以我认为他们强制执行它是为了降低复杂性,但运算符非静态仍然没有问题,只是更容易理解。I think C# enforces it because it makes better sense, not that it would be wrong.
It was hard for me to understand
this
parameter when adding 2 objects, so I think they enforce it to reduce complexity, but there's still nothing wrong with operators being non-static, just easier to understand.