在自定义运算符重载中,重载++操作,该如何区分前++和后++? 我菜鸟,请写出代码样例...先谢了...

解决方案 »

  1.   

    public class MyType
    {
     int _Value; public static MyType operator++(MyType a)
     {
      a._Value++;
      return a;
     }
    }
      

  2.   

    using System;namespace lianxi
    {
             
             class myclass
    {
    public int x,y;
    public myclass()
    {}
    public myclass(int a,int b)
    {
    x = a;
    y = b;
    }
    public static myclass operator ++(myclass z)
    {
                               z.x++;
    z.y++;
    return z;
    }
    }
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    class Class1
    {

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]

    static void Main(string[] args)
    {
    myclass xxx = new myclass(1,1);
    myclass yyy = xxx++;
    Console.WriteLine("x={0},y={1}",yyy.x,yyy.y);
    Console.WriteLine("x={0},y={1}",xxx.x,xxx.y);
                       }
               }
    }
    ------------------------输出结果------------------------
    x=2,y=2
    x=2,y=2
    我的想法应该是输出:
    x=1,y=1
    x=2,y=2我该怎么做??????
      

  3.   

    myclass yyy = xxx++;  ==============这不是=操作
    这句话的意思是 yyy和xxx是同一个实例.就是说,你改变任一个值, 另一个也会变重载=操作才能解决这个问题.
    myclass xxx= new myclass(1, 1);
    myclass yyy= new myclass(1,1);yyy= xxx++    ==============>  这是=操作
      

  4.   

    哦.知道了.. 谢谢你...wxdl1981(沉默之狼)