C++小程序 输出结果分析
#include <iostream>using namespace std;
class A
{
public:
A(){ cout<<"Ak1"; }
A(const A&){ cout<<"Ak2"; }
A& operator=(const A&) {cout<<"A=";return *this;}
~A(){ cout<<"Ad"; }
};
A& fun( A x){cout<<"fun";return x;}
int main()
{
A a;
cout<<"L1"<<endl;
A b = fun(a);
cout<<"L2"<<endl;
A& r = b;
cout<<"L3"<<endl;
A t= a;
return 0;
}
/*
输出结果:
AK1L1
AK2funAdAk2L2
L3
Ak2AdAdAd
*/
对于输出结果有点模棱两可。尤其是为什么执行A& r = b; 时,为什么不执行任何操作?