测试代码:
#include<iostream>
using namespace std;
class myclass{
int i;
public:
myclass(int x){i=x;cout<<"have make "<<i<<endl;}
~myclass(){cout<<"have del "<<i<<endl;}
void set(int x){i=x;}
int get(){return i;}
};
void fun(myclass x)
{
x.set(100);
cout<<"in function i is "<<x.get()<<endl;
}
int main()
{
myclass a(1);
cout<<"in main i is "<<a.get()<<endl;
fun(a);
cout<<"in main i is "<<a.get()<<endl;
return 0;
}
测试结果:
have make 1
in main i is 1
in function i is 100
have del 100
in main i is 1
have del 1
Press any key to continue
由此可见,在向函数传递类对象的时候,拷贝类对象时未进行构造函数,离开函数时却进行了析构函数!