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
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
解决方案 »
- C# winform 请求http
- asp.net里有login控件的,可是我在做Windows form窗体开发时找不到期login控件
- 如何用C#读取文件名变化的文本文件
- ====怎么样捕获Panel控件的KeyPress或者KeyDown、KeyUp等事件====
- 如何手动的想DataSet里面添加数据
- 怎样在毫秒级的时间内判断数据库是否可用 c# winform
- 求助(高分回报)关于新建一个项目时的,“恶意脚本提示”
- 如果您喜欢c#,如果您是c#高手,请进来------c#因为有了您们而精彩
- 我做的一个telnet程序,发送命令有问题!
- webform 和 winform 之间如何进行socket通信?
- DataGrid的按键屏蔽问题?
- 多谢剔教!!!两个C#程序都要打开同一个二进制文件进行读写,如何设置互斥体得以解决?
在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值才对,但为什么会有变化呢?
{
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
我想把这个问题发到MS的英文新闻组中,看看他们的意见,不知道你是否同意,注意是不想侵犯你的原创权? 同意,请回帖.
----------------------------------------
To teach a fish how to swim.
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.
您太客气了,我这个人的性格就是喜欢刨根问底,所以对这个问题多想了一些,不过自己实力有限,还请各位大虾悉心指点!
我同意您的要求,可以转贴!
我去您提到的页面看了一下,不过,这里好像只是讲解++和--运算符做为前缀和后缀运算符的使用规则的,并没有提到在程序员重载它们时可能遇到的情况,以及编译器背后的处理情况!
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
对不起,我还是没有明白您的意思!
您上面所说的这句话:“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值做出了判断也不一定呀!(当然,测试结果好像编译器并不是这样做的)
因此,我想找到这方面的官方资料,但不知道它在什么地方?还请各位大虾指点,谢!
在7.6.5 Prefix increment and decrement operators这一节中有详细的前缀形式的计算过程,以及前/后缀方式调用的区别,要仔细看。另外也要结合引用类型的特点自己思考一下为什么会导致这种结果。至于编译器幕后作的工作,本来就不是任何公开Document应该记载的。只要编译器的实现符合Spec的功能描述就可以,至于具体实现,你要自己去研究(用ildasm应该不难发现)。
Saucer提到的那段话的意思是说明,微软的.Net在处理++和--运算时,其编译器是如何解释的!我明白这个意思,但我不清楚,通过这一点就说明,我们在实现++重载的时候,就要通过我第一种方法那样,设一个中间对象,这样好让编译器根据实际情况++在前,还是++在后调用相应的新老对象,是吗?
:-)是的,如您所讲,关于编译器的细节问题,微软是不可能通过文档形式写出来,但是,对于这个问题,我认为它至少应该讲清楚:“实现这种++重载时,程序员一定要在重载方法内部内设一个中间对象,这样好让编译器根据实际情况调用之”,您说呢?
另外,像我提到的这个问题,除了在方法内部设定一中间对象外,还有其它办法实现吗?