下面代码 f()输出的字符串不是"qqq""www"之类.输出不正常.这是什么原理? gcc 4.8.5
#include<string>
#include<cstdio>
#include<iostream>
using namespace std;
const string &f(){
static int n=0;
static const char* const c[]={"qqq","www","eee","rrr","ttt","yyy","uuu","iii"};
return c[n++];
}
int main(int argc, char** argv)
{
cout << f() << endl;
cout << f() << endl;
cout << f()<< "111" << f() <<endl;
printf("%s%s%s\n",f().c_str(),"222",f().c_str());
printf("%s\n",f().c_str());
printf("%s\n",f().c_str());
return 0;
}
