为什么必须将重载运算符声明为public? [英] Why must overloaded operators be declared public?

查看:229
本文介绍了为什么必须将重载运算符声明为public?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个类中重载一个运算符,并且它是私有的,所以它只能在类中使用。

然而,当我试图编译时,我得到错误消息User -defined operator ...必须声明为static和public:

I wanted to overload an operator in a class , and have it private so that it could only be used from within the class.
However, when I tried to compile I got the error message "User-defined operator ... must be declared static and public:

为什么必须公开?

推荐答案

要回答问题的一半,你可能会看到Eric Lippert的博客。

To answer half part of your question you may see the blog post by Eric Lippert.

为什么重载运算符总是静态的in C#?

Why are overloaded operators always static in C#?


相反,当面对
潜在语言特性时,我们应该问自己:
的强大优势是否能证明所有的成本都合理?而且成本远远超过
,只是设计,开发,测试,
的文档和维护功能的平凡美元成本。有更微妙的成本,
喜欢,这个功能是否将更难以改变类型
的引用算法在未来?这会导致我们进入一个世界
,我们将无法进行更改,而不向后引入
兼容性中断?等等。

Rather, the question we should be asking ourselves when faced with a potential language feature is "does the compelling benefit of the feature justify all the costs?" And costs are considerably more than just the mundane dollar costs of designing, developing, testing, documenting and maintaining a feature. There are more subtle costs, like, will this feature make it more difficult to change the type inferencing algorithm in the future? Does this lead us into a world where we will be unable to make changes without introducing backwards compatibility breaks? And so on.

在这种特殊情况下,引人注目的好处很小。如果你想要
在C#中有一个虚拟分派的重载运算符,你可以非常容易地从静态部分构建一个
。例如:

In this specific case, the compelling benefit is small. If you want to have a virtual dispatched overloaded operator in C# you can build one out of static parts very easily. For example:



public class B {
    public static B operator+(B b1, B b2) { return b1.Add(b2); }
    protected virtual B Add(B b2) { // ...




那里有你。所以,好处是小。但是成本是
大。 C ++风格的实例运算符很奇怪。例如,他们打破
对称性。如果你定义一个运算符+,它接受一个C和一个int,那么
c + 2是合法的,但是2 + c不是,这严重打破了我们对
的直觉。 p>

And there you have it. So, the benefits are small. But the costs are large. C++-style instance operators are weird. For example, they break symmetry. If you define an operator+ that takes a C and an int, then c+2 is legal but 2+c is not, and that badly breaks our intuition about how the addition operator should behave.

对于您的问题的其他部分:为什么要公开

For your question's other part: Why they should be public.

我找不到任何真正的来源,但是IMO,如果他们不会被公开,并且只能在类中使用,那么这可以是使用简单的私有方法,而不是重载运算符。

I was not able to find any authentic source, but IMO, if they are not going to be public, and may be used inside a class only, then this can be done using a simple private method, rather than an overloaded operator.

您可能会看到RB Whitaker的博文

You may see the blog post by RB Whitaker

运算符重载


所有运算符重载必须是公共的和静态的,这应该使
有意义,因为我们想要访问整个
程序中的运算符,并且因为它属于整个类,而不是任何
特定类的实例。

All operator overloads must be public and static, which should make sense, since we want to have access to the operator throughout the program, and since it belongs to the class as a whole, rather than any specific instance of the class.

这篇关于为什么必须将重载运算符声明为public?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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