运算符重载和优先级 [英] Operator overloading and precedence

查看:210
本文介绍了运算符重载和优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中可以重载运营商,例如 + * 。在他们的数学解释,这些运营商有一个明确的优先顺序。

In C# you can overload operators, e.g. + and *. In their mathematical interpretation, these operators have a well defined order of precedence.

这是为了保持超载时,它以某种确定的方式发生改变,或者你可以重载优先顺序呢?

Is this order kept when overloading, does it change in some deterministic way, or can you overload the order of precedence as well?

推荐答案

如果你重载运算符,它将始终优先于默认实现。但是,你不能改变运营商本身的优先级,因此将保持为默认。在 MSDN 的更多信息。

If you overload an operator, it will always take precedence over the default implementation. However, you can't change the precedence of the operator itself, so it will be kept as default. More information on MSDN.

相关报价:

用户定义的运营商实现始终接管$优先b $ b预定义的运算符实现:仅当没有适用
用户自定义的运算符实现存在将预定义的
运营商实现被视为

User-defined operator implementations always take precedence over predefined operator implementations: Only when no applicable user-defined operator implementations exist will the predefined operator implementations be considered.

用户定义的运算符声明不能修改语法,
的优先级,或操作员的关联。例如,/
操作者始终是一个二进制运算符,总是具有在 =http://msdn.microsoft.com/en-us/library/aa691323% 28V = vs.71%29.aspx> 7.2.1 ,并且始终左结合。

User-defined operator declarations cannot modify the syntax, precedence, or associativity of an operator. For example, the / operator is always a binary operator, always has the precedence level specified in Section 7.2.1, and is always left-associative.

这篇关于运算符重载和优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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