|
#include<iostream>
using namespace std;
int main()
{
int i=3,j=3,a,b;
a=(i++)+(i++)+(i++);
b=(++j)+(++j)+(++j);
cout <<a<< endl;
cout <<b<< endl;
}
之前疾风同学问过这个问题,不过他b也和a一样,都是后加的,这个加在后面的确实好理解一点。
但因为好奇
所以我试了下这段,发现结果
b是16 这个b是16怎么来的又搞不明白了,就算是4+5+6,不也应该是15吗
(我在疾风同学帖子下面留言提出了这个疑问…但好像没人能看到…所以重发一个吧)
这是非常有名的一道题,目的是理解前++后++,考试经常会碰到!
- #include<iostream>
- using namespace std;
- int main()
- {
- int i = 3, j = 3, a, b;
- a = (i++) + (i++) + (i++); // a = 3 + 3 + 3
- b = (++j) + (++j) + (++j); // b = (j += 1, j += 1, j += 1) * 3
- cout << a << endl;
- cout << b << endl;
- cout << i << endl; // i 一共后 "++" 三次 == 6
- cout << j << endl; // j 一共前 "++" 三次 == 6
- // 验证
- int c = 3;
- int d = (c += 1, c += 1, c += 1) * 3;
- cout << d << endl; // 最终 d == 18
- }
复制代码
|
上一篇: 求助下一篇: 关于类的派生,我有一个问题,想问一下
|