64,651
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class A
{
private:
int data;
public:
A(){cout<<"默认构造"<<endl;}
A(int i):data(i){ cout<<"参数构造"<<data<<endl;}
A(const A &b){ data=b.data; cout<<"拷贝构造"<<data<<endl;}
~A(){ cout<<"析构"<<endl;}
};
A play(A a)
{
return a;
}
int main(void)
{
A temp;
temp=play(5);
return 0;
}
A play(A a)
{
return a;
}
这个函数改成成类似于这样
void paly(A& result_, A a)
{
result.A(a); //这里调用了传说中的拷贝构造函数
}
这里为返回值又构造了一个临时变量,所以出现了三次构造,三次析构 A temp = play(5);// A(const A &b)
temp = play(5);//operator=(const A& other)