2010-07-19 60 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 
    } 
} 
相关问题