2012年4月5日星期四

++++i和i++++的蛋疼问题

从标题就可以看出这个问题有多蛋疼了。平时谁会这么用啊啊啊啊!!!
不过,既然是个问题,总要有个答案,否则岂不抱憾终生?
通过google,在stackoverflow上寻到了答案

​T& operator ++(T& a);​     // pre
​T operator ++(T& a, int);​ // post
                          // note the int is solely to distinguish the two
也就是说,i++返回的是变量的值,++i返回的是变量的引用。因此i++++就相当于一个常数++,当然编译是没法通过的,而++++i就可以顺利通过。
但是,根据这个帖子,这两种表达式都会引发“未定义的行为”,因此“Don't do either of them.”
现在没什么遗憾了,这就是“朝闻道,夕死可以”的境界。
最后附上福利美图一枚。