当前位置: 代码迷 >> VC >> 你猜~C++菜鸟有关问题
  详细解决方案

你猜~C++菜鸟有关问题

热度:289   发布时间:2016-05-05 00:03:24.0
你猜~~~C++初学者问题
int test=2;
test=test++;
cout<<test<<endl; 
最后输出时多少?请附带解释~~~
------解决方案--------------------
应该是2。

这段代码等价:
int test = 2;
int t = test;
test = t;
test = test + 1;
test = t;
------解决方案--------------------
3

 mov         dword ptr [test],2 
 mov         eax,dword ptr [test] 
 mov         dword ptr [test],eax 
 mov         ecx,dword ptr [test] 
 add         ecx,1 
 mov         dword ptr [test],ecx 

------解决方案--------------------
输出结果是 3
------解决方案--------------------
是3
int test=2;
test=test++;(相当于test = 2)
cout<<test<<endl; (到了这里test++才执行完毕)
------解决方案--------------------
编译原理.......
------解决方案--------------------
不同的编译器,实现是不一样的

------解决方案--------------------
我想应该是2吧
------解决方案--------------------
为什么用test=test++呢?
用test++不行吗?
------解决方案--------------------
我感觉是2
++test才是3的吧
------解决方案--------------------
肯定是3的。

int test=2;//初始化为2
test=test++;//++操作是在一条语句结束后才执行的。相当于
<test = 2;
test++>

所以是3
 
  相关解决方案