简体   繁体   English

定义运算符+,=和+ =

[英]defining operator + , = and +=

I once read the following statement from a C++ notes, 我曾经从C ++注释中阅读以下语句,

In C++, defining operator + and = does not give the right meaning to +=. 在C ++中,定义运算符+和=不能赋予+ =正确的含义。 This language-design bug is fixed in C# 此语言设计错误已在C#中修复

I would like to know what exactly does this statement want to say? 我想知道这句话到底想说什么? Is that related to operator overload? 这与操作员过载有关吗?

I prefer C++ operator overloading mechanism. 我更喜欢C ++运算符重载机制。 It definitely not a design bug according to me. 对我来说,这绝对不是设计错误。

+ , = and += are three different operators. +=+=是三个不同的运算符。 If you want to use += you need to overload += . 如果要使用+= ,则需要重载+= Overloading + and = won't make += work. 重载+=将使+=无效。

I would like to add that in E1 += E2 E1 gets evaluated only once as far as C++ is concerned. 我想补充一点,在E1 += E2 ,就C ++而言, E1仅被评估一次。 I don't know the exact rules in C#. 我不知道C#中的确切规则。

It says, that in C# if you have overloaded operator + C# automatically will emulate operator += as combination of + and = ( a=a+b is equal to a+=b ). 它说,在C#中,如果您重载了运算符+,C#会自动将运算符+ =模拟为+和=的组合( a=a+b等于a+=b )。 In C++ it's not implemented, but it's not a bug. 在C ++中,它没有实现,但不是bug。 In C++ + and = doesn't give you += because mostly += works faster than +, because there is no need to create one more object. 在C ++中,+和=不会给您+ =,因为大多数情况下+ =的运行速度比+快,因为不需要再创建一个对象。

That's why mostly operator + is writen using += operator. 这就是为什么大多数运算符+是使用+ =运算符编写的。 Consider fallowing code: 考虑以下代码:

class foo
{
public:
   foo& operator+=(const foo& rhs)
   {
   //.......
   }
};
const foo operator+(const foo& lhs,const foo& rhs)
{
   foo temp = lhs;
   return temp+= rhs;
}

It means that in C++ if you defined your own operator + and operator = for your class, that still does not mean that your class will automatically support the += operator. 这意味着在C ++中,如果您为类定义了自己的运算符+和operator = ,这仍然并不意味着您的类将自动支持+=运算符。 If you want the += operator to work for your class, you have to define the += explicitly and separately. 如果要让+=运算符为您的类工作,则必须显式且单独定义+=

In C#, if I understood it correctly, defining operators + and = for your class will also mean that you'll be able to use operator += with your class. 在C#中,如果我理解正确的话,则为您的类定义运算符+=也将意味着您将可以在类中使用运算符+= The += will be "emulated" through combination of operator + and operator = . +=将通过组合运算符+和运算符=来“模拟”。 Eg expression a += b will be interpreted as a = a + b . 例如,表达式a += b将被解释为a = a + b

It doesn't work that way in C++. 在C ++中,这种方式不起作用。 If you don't define the += explicitly, a += b will result in compiler error, even if you have + and = defined. 如果没有显式定义+= ,则即使已定义+=a += b也会导致编译器错误。

C# does not allow operator overloading = since it does not allow direct pointer management. C#不允许运算符重载=,因为它不允许直接指针管理。 Its behavior is fixed based on whether it is reference or value type. 它的行为基于引用还是值类型而固定。 For the same reason you cannot overload += . 出于同样的原因,您不能重载+ =。 It's meaning will always be doing the sum and assignment. 这意味着将始终进行求和与赋值。 You can only therefore decide what the meaning for + is to your datastructure. 因此,您只能确定+对您的数据结构意味着什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM