后递增运算符重载 [英] Post-increment Operator Overloading

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

问题描述

我在想重载在C#中后增量运算符的问题。使用整数我们得到了以下结果。

I'm having problems trying to overload the post increment operator in C#. Using integers we get the following results.

int n;

n = 10;
Console.WriteLine(n); // 10
Console.WriteLine(n++); // 10
Console.WriteLine(n); // 11

n = 10;
Console.WriteLine(n); // 10
Console.WriteLine(++n); // 11
Console.WriteLine(n); // 11

但是,当我尝试使用类,它看起来像对象交换。

But, when I try it using classes, it looks like the objects are exchanged.

class Account
{
    public int Balance { get; set; }
    public string Name { get; set; }

    public Account(string name, int balance)
    {
        Balance = balance;
        Name = name;
    }

    public override string ToString()
    {
        return Name + " " + Balance.ToString();
    }

    public static Account operator ++(Account a)
    {
        Account b = new Account("operator ++", a.Balance);
        a.Balance += 1;
        return b;
    }

    public static void Main()
    {
        Account a = new Account("original", 10);

        Console.WriteLine(a); // "original 10"

        Account b = a++;

        Console.WriteLine(b); // "original 11", expected "operator ++ 10"
        Console.WriteLine(a); // "operator ++ 10", expected "original 11"
    }
}

调试应用程序,重载运算符方法,返回旧值(10),并已通过引用传递对象的新对象,将新的值(11),但最后的对象被交换。这究竟是为什么?

Debugging the application, the overloaded operator method, returns the new object with the old value (10) and the object that has been passed by reference, has the new value (11), but finally the objects are exchanged. Why is this happening?

推荐答案

关键是理解怎么行帐户B = A ++; 的作品。鉴于你的code是怎么写的,这条线是本等价的:

The key is in understanding how the line Account b = a++; works. Given how your code is written, this line is the equivalent of this:

Account b = a;
a++;

和即它将执行顺序。分配有效地(1)的增量之前发生。所以,这行的第一个效果是的 A 的指的是原始对象的

And that is the order it will execute in. The assignment effectively(1) happens before the increment. So, the first effect of this line is that a and b both refer to the original object a.

现在的++部分进行评估。内部操作方法,我们增加原始对象的余额。在这一点上的 A 的都指向原来的,用一个余额 11和 b 的将继续这样做。

Now the ++ portion will be evaluated. Inside of the operator method, we increment the Balance of the original object. At this point a and b are both pointing at the original, with a Balance of 11, and b will continue to do so.

不过,您已经创建操作方法中一个新的对象,并返回它作为操作的输出。的的将现在新创建的对象进行更新,以点。

However, you've created a new object inside the operator method and returned it as the output of the operator. a will now be updated to point at the newly created object.

因此​​,的现在指向一个新的对象,而的继续指向原始。这就是为什么输出的WriteLine出现互换。

So, a now points to a new object, while b continues to point to the original. That's why the WriteLine output appears swapped.

正如@MarkusQ指出的,++运算是指做就地改性。通过生成一个新的对象,你打破这个假设。操作符重载的对象是一个棘手的问题,这也是为什么它在大多数情况下最好避免一个很好的例子。

As @MarkusQ pointed out, the ++ operator is meant to do in-place modification. By generating a new object, you're breaking that assumption. Operator overloading on objects is a tricky subject, and this is an excellent example of why it's better avoided in most cases.


1 - 的只是为了准确起见,分配实际上并没有增量前与运营商的对象打交道时发生,但最终的结果是在这种情况下是相同的。实际上,原始对象引用被复制,则该操作被执行的原稿,然后将复制的参照被分配给左侧的变量。这只是更容易解释,如果你pretend的分配首先发生。

什么是实际发生的是这样的:

What's really happening is that this:

Account b = a++;

结果在此,由于++运算是如何工作的对象

results in this, due to how the ++ operator works on objects:

Account copy = a;

Account x = new Account("operator ++", a.Balance);
a.Balance += 1; // original object's Balance is incremented
a = x; // a now points to the new object, copy still points to the original

Account b = copy; // b and copy now point at the same, original, object

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

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