2010-07-19 30 views
2

我想重載++運算符在我的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(); 
     } 
    } 
} 
+1

你可以發佈你的代碼爲你的前後增量,所以我們可以看到是否有問題? – JLWarlow 2010-07-19 15:46:19

+4

向我們顯示您的代碼。 – dthorpe 2010-07-19 15:46:29

+0

請現在回答。我已經上傳了我的代碼。 – chanchal1987 2010-07-19 16:20:38

回答

5

你的樣品未能實現此一元運算符正確,如C#說明書中17.9.1一元運算符指定:

在C++中不同的是,這種方法不需要, 和,實際上不應該,直接修改其操作數的值 。

下面是一些微單元測試你的樣本:

using System; 

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() 
    { 
     var a = new ABC(5, 6); 
     if ((a.a != 5) || (a.b != 6)) Console.WriteLine(".ctor failed"); 

     var post = a++; 
     if ((a.a != 6) || (a.b != 7)) Console.WriteLine("post incrementation failed"); 
     if ((post.a != 5) || (post.b != 6)) Console.WriteLine("post incrementation result failed"); 

     var pre = ++a; 
     if ((a.a != 7) || (a.b != 8)) Console.WriteLine("pre incrementation failed"); 
     if ((pre.a != 7) || (pre.b != 8)) Console.WriteLine("pre incrementation result failed"); 

     Console.Read(); 
    } 
} 

您的代碼失敗是後增量的結果,這是由於這一事實,你改變ABC作爲參數,而不是通過實例返回一個新的實例。更正後的代碼:

class ABC 
{ 
    public int a,b; 
    public ABC(int x, int y) 
    { 
    a = x; 
    b = y; 
    } 

    public static ABC operator ++(ABC x) 
    { 
    return new ABC(x.a + 1, x.b + 1); 
    } 
} 
+0

謝謝。此代碼正在工作。 – chanchal1987 2010-07-19 16:59:44

+2

天啊!這樣的記憶浪費! – Paul 2016-02-17 20:28:36

1

下面是從規範樣本,什麼你的情況有什麼不同?

public class IntVector 
{ 
    public int Length { … } // read-only property 
    public int this[int index] { … } // read-write indexer 
    public IntVector(int vectorLength) { … } 
    public static IntVector operator++(IntVector iv) { 
     IntVector temp = new IntVector(iv.Length); 
     for (int i = 0; i < iv.Length; ++i) 
      temp[i] = iv[i] + 1; 
     return temp; 
    } 
} 
class Test 
{ 
    static void Main() { 
     IntVector iv1 = new IntVector(4); // vector of 4x0 
     IntVector iv2; 

     iv2 = iv1++; // iv2 contains 4x0, iv1 contains 4x1 
     iv2 = ++iv1; // iv2 contains 4x2, iv1 contains 4x2 
    } 
}