来自:http://blog.csdn.net/sailor_8318/archive/2008/11/23/3357766.aspx
IBM的 一道题
char* fun1() { cout<<"a"; return "1"; }
char* fun2() { cout<<"b"; return "1"; }
char* fun3() { cout<<"c"; return "1"; }
int main(int argc, char* argv[])
{
cout<<"m"<<fun1()<<fun2()<<fun3()<<endl;
return 0;
}
屏幕输出是多少?
cbam111
为什么不是abcm111呢?或者是ma1b 1c 1呢?
嵌套,递归的感觉
引出:编c/c++程序有一个纪律:一个语句中不要有两个表达式有副作用。
典型的这类行为包括:b=(a++)+(a++)+(a++);
因此,不要在C语言里面做这种事情:
char* fun() { cout<<"q"; return ""; }
cout<<"m"<<fun()<<fun()<<fun()<<endl;
要这样:
char* fun() { return "q"; }
cout<<"m"<<fun()<<fun()<<fun()<<endl; // 输出的一定是 "mqqq/n"
这样更好:
string fun() { return string("q"); }
cout<<"m"<<fun()<<fun()<<fun()<<endl; // 输出的一定是 "mqqq/n"
这样就更好了:
string f="q"; // 隐式转换
cout<<"m"<<fun()<<fun()<<fun()<<endl; // 输出的一定是 "mqqq/n"