C++静态变量和cout

来源:互联网 发布:大学生网络惨案 编辑:程序博客网 时间:2024/05/17 17:41
#include<iostream>
using namespace std;
int  add(int x)
{
static int n=0;
n=n+x;
// n=x;
cout<<n<<"a";//1
return n;
}
int main()
{
int i=1;
int j=2;
cout<<add(i)<<add(j)<<endl;//2
// cout<<add(i);//3
// cout<<add(j);
return 0;

}

输出答案为2a3a32

对于cout来说,总是从右往左压入缓冲区,所以上题是从右压入缓冲区,n=0+j=2,然后是add(i)压入缓冲区,最后把结果输出32,

注意在压入缓冲区时,add里的cout就会输出。

0 0