问题
以下代码为什么只进行一次拷贝代码,不是返回值跟a不是同一个,是另一个同类不同对象,由a进行拷贝拷贝函数得,p c=work()不是跟p c=同类对象 一样要再把右侧对象进行拷贝函数得吗?
代码
#include <iostream>
using namespace std;
class p {
int dp;
public:
p(){
cout << "1" << endl;
}
p(const p& o) {
cout << '3' << endl;
dp = o.dp;
}
~p() {
cout << "2" << endl;
}
};
p work() {
p a;
cout << (int)&a << endl;
return a;
}
int main()
{
p c = work();
}