1.拷贝构造函数
拷贝构造函数是构造函数的一种。当利用已存在的对象创建一个新对象时,就会调用新对象的拷贝构造函数进行初始化。
拷贝构造函数的格式是固定的,即接收一个const引用作为参数。
拷贝构造函数一般不要声明成 explicit。
如果自己定义了拷贝构造函数,那么系统默认的拷贝操作就不存在了,此时必须指定拷贝方式,即必须在自己的拷贝构造函数中手动给类成员赋值,以免出现类成员没有被赋值就被使用的情况。
#include <iostream>
using namespace std;
class Car
{
public:
int m_price;
int m_length;
Car(int price = 0, int length = 0) : m_price(price), m_length(length)
{
cout << "调用了Car的构造函数" << endl;
}
Car(const Car& car) : m_price(car.m_price), m_length(car.m_length)
{
cout << "调用了Car的拷贝构造函数" << endl;
}
void display()
{
cout << "price = " << m_price << ", length = " << m_length << endl;
}
};
int main()
{
Car car1(100, 5);
Car car2(car1);
car2.display();
return 0;
}
输出结果如下:
汇编代码如下:
2.默认的拷贝操作
默认情况下,类对象的拷贝是每个成员变量逐个拷贝。
在下面代码中,我们没有为 Car
类定义拷贝构造函数,从图中可以看出,编译器并没有为 Car
类生成一个默认拷贝构造函数,而是直接给每个成员变量逐个拷贝赋值。
#include <iostream>
using namespace std;
class Car
{
public:
int m_price;
int m_length;
Car(int price = 0, int length = 0) : m_price(price), m_length(length)
{
cout << "调用了Car的构造函数" << endl;
}
void display()
{
cout << "price = " << m_price << ", length = " << m_length << endl;
}
};
int main()
{
Car car1(100, 5);
Car car2(car1);
car2.display();
return 0;
}
输出结果如下:
汇编代码如下:
3.默认拷贝构造函数
与 默认构造函数 类似,只有在某些特定的情况下,编译器才会给类自动生成一个默认拷贝构造函数。
在下面代码中,我们没有为 Person
类定义拷贝构造函数,但 Person
类中包含了一个 Car
类对象且这个 Car
类对象有拷贝构造函数,此时编译器会为 Person
类自动生成一个默认拷贝构造函数。
#include <iostream>
using namespace std;
class Car
{
public:
int m_price;
Car(int price = 0) : m_price(price)
{
cout << "调用了Car的构造函数" << endl;
}
Car(const Car& car) : m_price(car.m_price)
{
cout << "调用了Car的拷贝构造函数" << endl;
}
void display()
{
cout << "price = " << m_price << endl;
}
};
class Person
{
public:
int m_age;
Car car;
};
int main()
{
Person person1;
Person person2(person1);
return 0;
}
输出结果如下:
汇编代码如下:
在下面代码中,Person
类中包含了一个 Car
类对象且这个 Car
类对象有拷贝构造函数,我们手动为 Person
类定义拷贝构造函数,此时就会调用我们自定义的拷贝构造函数。
#include <iostream>
using namespace std;
class Car
{
public:
int m_price;
Car(int price = 0) : m_price(price)
{
cout << "调用了Car的构造函数" << endl;
}
Car(const Car& car) : m_price(car.m_price)
{
cout << "调用了Car的拷贝构造函数" << endl;
}
void display()
{
cout << "price = " << m_price << endl;
}
};
class Person
{
public:
int m_age;
Car car;
Person(int age = 0, int price = 0) : m_age(age), car(price)
{
cout << "调用了Person的构造函数" << endl;
}
Person(const Person& person) : m_age(person.m_age), car(person.car)
{
cout << "调用了Person的拷贝构造函数" << endl;
}
};
int main()
{
Person person1;
Person person2(person1);
return 0;
}
输出结果如下:
汇编代码如下:
4.何时调用拷贝构造函数
拷贝构造函数和拷贝赋值运算符的行为比较相似,都是将一个对象的值复制给另一个对象,但是其结果却有些不同,拷贝构造函数使用传入对象的值生成一个新的对象的实例,而拷贝赋值运算符是将对象的值复制给一个已经存在的实例。这种区别从两者的名字也可以很轻易地分辨出来,拷贝构造函数也是一种构造函数,那么它的功能就是创建一个新的对象实例;拷贝赋值运算符是执行某种运算,将一个对象的值复制给另一个对象(已经存在的)。
调用的是拷贝构造函数还是拷贝赋值运算符,主要是看是否有新的对象实例产生。如果产生了新的对象实例,那调用的就是拷贝构造函数;如果没有,那就是对已有的对象赋值,调用的是拷贝赋值运算符。
调用拷贝构造函数主要有以下场景:
- 使用一个对象给另一个对象初始化
- 对象作为函数的参数,以值传递的方式传给函数
- 对象作为函数的返回值,以值的方式从函数返回
#include <iostream>
using namespace std;
class Student
{
private:
int age;
public:
Student()
{
age = 0;
cout << "调用了无参的构造函数 - " << this << endl;
}
Student(int v)
{
age = v;
cout << "调用了带参的构造函数 - " << this << endl;
}
Student(const Student& stu) // 拷贝构造函数
{
age = stu.age;
cout << "调用了拷贝构造函数 - " << this << endl;
}
Student& operator=(const Student& stu) // 拷贝赋值运算符
{
age = stu.age;
cout << "调用了拷贝赋值运算符 - " << this << endl;
return *this;
}
~Student()
{
cout << "调用了析构函数 - " << this << endl;
}
void display()
{
cout << "调用了display函数 - " << this << endl;
}
};
void fun1(Student stu)
{
return;
}
Student fun2()
{
Student stu;
return stu;
}
int main()
{
Student stu1(18);
cout << "------------------------------" << endl;
Student stu2(stu1);
cout << "------------------------------" << endl;
Student stu3 = stu1;
cout << "------------------------------" << endl;
Student stu4;
stu4 = stu1;
cout << "------------------------------" << endl;
fun1(stu1);
cout << "------------------------------" << endl;
fun2();
cout << "------------------------------" << endl;
stu4 = fun2();
cout << "------------------------------" << endl;
Student stu5 = fun2();
cout << "------------------------------" << endl;
return 0;
}
输出结果如下: