问题及代码:
#include <iostream>
using namespace std;
class example
{
public:
example()
{
cout<<"Default Constructing! "<<endl;
}
example(int n)
{
i=n;
cout<<"Constructing: "<<i<<endl;
}
~example()
{
cout <<"Destructing: "<<i<<endl;
}
int get_i()
{
return i;
}
private:
int i;
};
int sqr_it(example o)
{
return o.get_i()* o.get_i();
}
int main()
{
example x(10); //执行一次带默认参数的构造函数,i=10 输出 Constructing: 10
cout<<x.get_i()<<endl; //输出 10
cout<<sqr_it(x)<<endl; //在x.get_i()函数中,新建一个example o 执行一次复制构造函数,将x的数据复制给o,然后输出 100
//函数调用结束,进行一次o的析构函数,输出 Destructing:10
return 0; //程序结束,执行x的析构函数输出 Destructing: 10
}
运行结果:
知识点总结:
对象的复制,执行复制构造函数