C++函数返回的临时对象用来初始化类对象是否调用拷贝构造函数
最近,小编在备战考试的时候,被一个问题纠结了许久。最近获得老师启发,终于搞明白了临时对象初始化类对象调用拷贝构造函数的问题!下面是小编解疑过程中的代码:
#include <iostream>
using namespace std;
class Point
{
public:
Point(int=0,int=0);
Point (const Point&);
void displayxy();
~Point();
private:
int X,Y;
};
//点类的实现部分
Point::Point (int x,int y)
{
X=x;
Y=y;
cout<<"Constructor is called! "; cout<<this<<endl;
displayxy();
}
Point::Point(const Point& p){
X=p.X;
Y=p.Y;
cout<<"Copy constructor is called! "; cout<<this<<endl;
displayxy();
}
Point::~Point(){
cout<<"Destructor is called! "; cout<&