谁能帮我解释一下这个C++ 程序
#include "iostream.h"
class CA
{
int* a;
public:
friend class CB;
CA(int x)
{
a=new int[5];
*a=x;
}
CA()
{
a=new int[5];
*a=0;
}
~CA()
{
cout<<"析构被调用"<<endl;
*a=*a-1;
}
int get_a()
{
return *a;
}
};
class CB
{
public:
int get(CA test)
{
return *test.a;
}
};
int main(int argc,char* argv[])
{
CA test(2);
CB test2;
cout<<test.get_a()<<endl;
cout<<test2.get(test)<<endl;
cout<<test2.get(test)<<endl;
return 0;
}
我觉的应该是输出 2,1,0 但却是输出2,2,1
[解决办法]
上面是运行结果,解释如下:
CA test(2);
CB test2;
cout<<test.get_a()<<endl;
cout<<test2.get(test)<<endl;
cout<<test2.get(test)<<endl;