temp is an object reference, when you do
temp.a ++;
return temp;you update the value inside temp, so when you call
m++.a or (++m).a, you increment m's a in both cases, that is why you have 1

解决方案 »

  1.   

    saucer说的不错,我后面改的代码的确如你所述。
         在C#中同C++中运算符重载一样,其实隐藏在背后的是方法的调用,上面的代码:
           public static My operator ++(My temp)
           {
             My k = new My();
    k.a = temp.a + 1;
    return k;
           }
         其实,每次当我们调用++m 或者 m++的时候都会调用其上的方法内容。也就是说返回了同一对象。在退出后似乎应该具有相同的(++m).a或者
    (m++).a值才对,但为什么会有变化呢?
      

  2.   

    the reasons why the following works:public static My operator++(My temp)
    {
             My k = new My();
    k.a = temp.a + 1;
    return k;
    }it returns a new object of My, if you do (m++).a, the system knows to use the value inside the old object, if you do (++m).a, the system knows to use the value inside the new objectbut when you do
    public static My operator ++(My temp)
    {
      temp.a++;
      return temp;
    }you just changed the value inside temp, the old object is same as the new object, they points to the same memory, that is why (m++).a and (++m).a are same
      

  3.   

    感谢saucer的回答,对于这个问题,我开始也认为是编译器在背后做了一些处理,但是我在帮助中没有查到有关这个问题的微软官方解释,您知道在什么地方可以看到这方面的内容吗?
      

  4.   

    Archerfl: 你的这个问题非常厉害,你发出来得时候,我就在我的计算机上试过了,但不执行了好几次,自己一直也在奇怪.当天下午就没敢发音.相对与其他的一些浮躁的问题来说,我觉得想你这种扎实的人在这个论坛上实在太少了.
    我想把这个问题发到MS的英文新闻组中,看看他们的意见,不知道你是否同意,注意是不想侵犯你的原创权? 同意,请回帖.
    ----------------------------------------
    To teach a fish how to swim.
      

  5.   

    according to C# Language Specification
    http://www.csharpfriends.com/Members/main/spec/index.aspx?specID=14.6.5.htm1 The ++ and --operators also support postfix notation (§14.5.9). 2 The result of x++ or x--is the value of x before the operation, whereas the result of ++x or --x is the value of x after the operation. 3 In either case, x itself has the same value after the operation. 
     
      

  6.   

    To TheAres:
         您太客气了,我这个人的性格就是喜欢刨根问底,所以对这个问题多想了一些,不过自己实力有限,还请各位大虾悉心指点!
         我同意您的要求,可以转贴!
      

  7.   

    To saucer:
        我去您提到的页面看了一下,不过,这里好像只是讲解++和--运算符做为前缀和后缀运算符的使用规则的,并没有提到在程序员重载它们时可能遇到的情况,以及编译器背后的处理情况!
      

  8.   

    if you read this line carefully:The result of x++ or x--is the value of x before the operation, whereas the result of ++x or --x is the value of x after the operation.you should understand how it works:if you have
    public static My operator ++(My temp)
    {
      temp.a++;
      return temp;
    }and call either (++m).a or (m++).a, the value of m (a pointer to some managed memory) is same before and after ++ operation, but you changed the value of "a" inside m 
      

  9.   

    To saucer:
        对不起,我还是没有明白您的意思!
        您上面所说的这句话:“The result of x++ or x--is the value of x before the operation, whereas the result of ++x or --x is the value of x after the operation.”的意思是讲解++和--运算符的使用规则的。不错,您后面引述我的代码,这里确实在方法内部对同一对象的字段值 a 进行了改写,但这似乎并不能说明最后就可能会影响(++m).a和(m++).a的返回值吧,因为我们并不知道编译器在最后做了些什么,也许,编译器可能已经记住了m.a改变前的值,并根据语义对++m和m++后的.a值做出了判断也不一定呀!(当然,测试结果好像编译器并不是这样做的)
         因此,我想找到这方面的官方资料,但不知道它在什么地方?还请各位大虾指点,谢!
        
      

  10.   

    我觉得Saucer指出的那一段已经说得很明了了。
    在7.6.5 Prefix increment and decrement operators这一节中有详细的前缀形式的计算过程,以及前/后缀方式调用的区别,要仔细看。另外也要结合引用类型的特点自己思考一下为什么会导致这种结果。至于编译器幕后作的工作,本来就不是任何公开Document应该记载的。只要编译器的实现符合Spec的功能描述就可以,至于具体实现,你要自己去研究(用ildasm应该不难发现)。
      

  11.   

    To qqchen79:
       Saucer提到的那段话的意思是说明,微软的.Net在处理++和--运算时,其编译器是如何解释的!我明白这个意思,但我不清楚,通过这一点就说明,我们在实现++重载的时候,就要通过我第一种方法那样,设一个中间对象,这样好让编译器根据实际情况++在前,还是++在后调用相应的新老对象,是吗?
       :-)是的,如您所讲,关于编译器的细节问题,微软是不可能通过文档形式写出来,但是,对于这个问题,我认为它至少应该讲清楚:“实现这种++重载时,程序员一定要在重载方法内部内设一个中间对象,这样好让编译器根据实际情况调用之”,您说呢?
       另外,像我提到的这个问题,除了在方法内部设定一中间对象外,还有其它办法实现吗?