拷贝构造是构造函数的重载形式,参数只有一个,必须是当前类的对象
当我们使用一个已经存在的对象作为参数,去初始化另一个对象的时候,编译器会调用拷贝构造
初始化是显式的调用拷贝构造
当使用对象进行值传递的时候,会发生隐式的调用拷贝构造
一、显式调用:使用已存在的对象初始化另一个对象
class Date
{
private:
int _year;
int _month;
int _day;
public:
Date(int year=0,int month=0,int day=0)
{
_year = year;
_month = month;
_day = day;
}
Date(Date& d) //这里必须使用引用,否则就会变成 值传递对象
{
cout << "拷贝构造函数被调用" << endl;
}
};
int main() {
Date d1;
Date d2(d1); //使用已经存在的d1对象初始化d2对象
return 0;
}
二、隐式调用:值传递 调用拷贝构造函数
由于值传递会触发调用拷贝构造函数,这就是上面在定义拷贝构造函数的时候,形参必须添加引用的原因,否则会触发拷贝构造函数的无限调用
1、参数 值传递
这里定义了一个 赋值运算符重载函数,参数使用的是 值传递
我们在类中新增下面的函数
void operator=(Date d) //注意这里是 值传递
{
_year = d._year;
_month = d._month;
_day = d._day;
}
稍微修改一下main函数,然后运行
int main() {
Date d1;
Date d2;
d2 = d1; //编译时会被转化为 d2.operator=(d1);
return 0;
}
但是如果 void operator=(Date d) 改为 void operator=(Date& d)
此时就是引用传递,不会调用拷贝构造函数
2、返回值 值传递
我们把上面的 赋值运算符重载函数 void operator=(Date& d) 改为下面的样子
Date operator=(Date& d) //注意这里是 值传递
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
main函数保持不变,然后运行
同理 Date operator=(Date& d) 改为 Date& operator=(Date& d)
传值返回 改为 引用返回就能避免拷贝构造函数被调用