我想重載++
運算符在我的c#類中使用運算符重載使用前增量和後增量。但只有後增加工作。如何使這兩個功能在我的課堂上工作? 假如我做了一個ABC類像 -如何在我的課堂上實現前後增量/減量運算符?
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
class ABC
{
public int a,b;
public ABC(int x, int y)
{
a = x;
b = y;
}
public static ABC operator ++(ABC x)
{
x.a++;
x.b++;
return x;
}
}
class Program
{
static void Main(string[] args)
{
ABC a = new ABC(5, 6);
ABC b, c;
b = a++;
Console.WriteLine("After post increment values are {0} and {1} and values of b are {2} and {3}", a.a, a.b, b.a, b.b);// expected output a.a = 6, a.b = 7, b.a = 5, b.b = 6 but not get that
c = ++a;
Console.WriteLine("After pre increment values are {0} and {1} and values of c are {2} and {3}", a.a, a.b, c.a, c.b); // expected output a.a = 7, a.b = 7, c.a = 7, c.b = 8 works fine
Console.Read();
}
}
}
你可以發佈你的代碼爲你的前後增量,所以我們可以看到是否有問題? – JLWarlow 2010-07-19 15:46:19
向我們顯示您的代碼。 – dthorpe 2010-07-19 15:46:29
請現在回答。我已經上傳了我的代碼。 – chanchal1987 2010-07-19 16:20:38