【深入理解C++】调用父类的拷贝构造函数

本文探讨了C++中拷贝构造函数在继承体系下的行为,包括默认拷贝操作、显式调用父类拷贝构造函数及用子类对象初始化父类对象时的现象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

输出结果如下:

在这里插入图片描述

汇编代码如下:

在这里插入图片描述

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;
}

输出结果如下:

在这里插入图片描述

如果 Student 的拷贝构造函数没有调用父类的拷贝构造函数,即只给 m_score 赋值而不管 m_age,会发生什么现象?

答:因为子类的构造函数会默认调用父类的无参构造函数,所以 m_age0 而不是 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;
}

输出结果如下:

在这里插入图片描述

汇编代码如下:

在这里插入图片描述

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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值