#include <iostream>
int counter = 0;
int f(){
return counter++;
}
int main(){
std::cout << f(); //output = 0
std::cout << f()+f(); // output = 1;
return 0;
}
这可能是个愚蠢的问题,但为什么 f()
在第一种情况下不等于 1 而不是 0?
我以为 counter++
和 counter = counter + 1
一样?
最佳答案
没有。 counter++
更接近 (counter = counter + 1) - 1
关于c++ - 全局变量增量如何在 C++ 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43404010/