1.默认的拷贝操作
默认情况下,继承体系下类对象的拷贝是每个成员变量逐个拷贝。
#include <iostream>
using namespace std;
class Person
{
private:
int m_age;
public:
Person(int age = 0) : m_age(age)
{
cout << "调用了Person的构造函数" << endl;
}
};
class Student : public Person
{
private:
int m_score;
public:
Student(int age = 0, int score = 0) : Person(age), m_score(score)
{
cout << "调用了Student的构造函数" << endl;
}
};
int main()
{
Student stu1(18, 100);
Student stu2(stu1);
return 0;
}
输出结果如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/2bb8cfd3a61a4891a749b71af2b8bfb3.png#pic_center)
汇编代码如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/b96c3d22c2b54a67b121362d6cb36476.png#pic_center)
2.调用父类的拷贝构造函数
如下代码所示,可以在子类中自定义拷贝构造函数,并在初始化列表中调用父类的拷贝构造函数为父类部分的成员变量赋值。
#include <iostream>
using namespace std;
class Person
{
private:
int m_age;
public:
Person(int age = 0) : m_age(age)
{
cout << "调用了Person的构造函数" << endl;
}
Person(const Person& person) : m_age(person.m_age)
{
cout << "调用了Person的拷贝构造函数" << endl;
}
};
class Student : public Person
{
private:
int m_score;
public:
Student(int age = 0, int score = 0) : Person(age), m_score(score)
{
cout << "调用了Student的构造函数" << endl;
}
Student(const Student& student) : Person(student), m_score(student.m_score)
{
cout << "调用了Student的拷贝构造函数" << endl;
}
};
int main()
{
Student stu1(18, 100);
Student stu2(stu1);
return 0;
}
输出结果如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/6961b40b965f4f72af63b32dcd7d8d4b.png#pic_center)
如果 Student
的拷贝构造函数没有调用父类的拷贝构造函数,即只给 m_score
赋值而不管 m_age
,会发生什么现象?
答:因为子类的构造函数会默认调用父类的无参构造函数,所以 m_age
是 0
而不是 0xCC
。
#include <iostream>
using namespace std;
class Person
{
private:
int m_age;
public:
Person(int age = 0) : m_age(age)
{
cout << "调用了Person的构造函数" << endl;
}
Person(const Person& person) : m_age(person.m_age)
{
cout << "调用了Person的拷贝构造函数" << endl;
}
};
class Student : public Person
{
private:
int m_score;
public:
Student(int age = 0, int score = 0) : Person(age), m_score(score)
{
cout << "调用了Student的构造函数" << endl;
}
Student(const Student& student) : m_score(student.m_score)
{
cout << "调用了Student的拷贝构造函数" << endl;
}
};
int main()
{
Student stu1(18, 100);
Student stu2(stu1);
return 0;
}
输出结果如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/4cbd351923dc4fd886010879bcea91c6.png#pic_center)
汇编代码如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/02f03dcfca644873a9b7978fe0fb483b.png#pic_center)
3.用子类对象初始化父类对象
用派生类对象初始化基类对象时,只有该派生类对象的基类部分会被拷贝,派生类部分将会被忽略掉。
#include <iostream>
using namespace std;
class Person
{
private:
int m_age;
public:
Person(int age = 0) : m_age(age)
{
cout << "调用了Person的构造函数" << endl;
}
Person(const Person& person) : m_age(person.m_age)
{
cout << "调用了Person的拷贝构造函数" << endl;
}
};
class Student : public Person
{
private:
int m_score;
public:
Student(int age = 0, int score = 0) : Person(age), m_score(score)
{
cout << "调用了Student的构造函数" << endl;
}
Student(const Student& student) : Person(student), m_score(student.m_score)
{
cout << "调用了Student的拷贝构造函数" << endl;
}
};
int main()
{
Student stu(18, 100);
Person person(stu); // 此处会调用Person的拷贝构造函数。但是Person作为基类,它只能处理基类自己的成员,无法处理派生类中的成员。
return 0;
}