子类拷贝构造函数会调用父类拷贝构造函数吗?

一. 编译器提供的默认子类拷贝构造函数会调用父类拷贝构造函数。

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

class Parent {
public:
    Parent(string home_address = "中国") : m_home_address(home_address) {
        cout << "调用父类构造函数" << endl;
    }
    Parent(const Parent &other) {
        m_home_address = other.m_home_address;
        cout << "调用父类拷贝构造函数" << endl;
    }
    string get_home_address() {return m_home_address;}
private:
    string m_home_address;
};

class Child : public Parent {
public:
    Child(string home_address = "") : Parent(home_address) {
        cout << "调用子类构造函数" << endl;
    }
private:
};

int main(int argc, char *argv[]) {
    Child c1("广州");
    cout << "c1 home_address = " << c1.get_home_address() << endl << endl;

    Child c2 = c1; // 调用了子类的默认拷贝构造函数
    cout << "c2 home_address = " << c2.get_home_address() << endl;
    return 0;
}

从打印的信息中,我们可以知道:

  1. 创建一个对象 c1 时,先调用父类构造函数,后调用子类构造函数。

  2. 创建 c2 对象时,执行了编译器提供的默认子类拷贝构造函数,具体构造顺序为:先调用父类拷贝构造函数,后调用编译器提供的默认子类拷贝构造函数。从而打印出 c2 home_address = 广州

二. 重写的子类拷贝构造函数默认不会调用父类的拷贝构造函数,而是调用父类默认构造函数。

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

class Parent {
public:
    Parent(string home_address = "中国") : m_home_address(home_address) {
        cout << "调用父类构造函数" << endl;
    }
    Parent(const Parent &other) {
        m_home_address = other.m_home_address;
        cout << "调用父类拷贝构造函数" << endl;
    }
    string get_home_address() {return m_home_address;}
private:
    string m_home_address;
};

class Child : public Parent {
public:
    Child(string home_address = "") : Parent(home_address) {
        cout << "调用子类构造函数" << endl;
    }
    Child(const Child &other) {
        cout << "调用子类拷贝构造函数" << endl;
    }
private:
};

int main(int argc, char *argv[]) {
    Child c1("广州");
    cout << "c1 home_address = " << c1.get_home_address() << endl << endl;

    Child c2 = c1; // 调用了子类的默认拷贝构造函数
    cout << "c2 home_address = " << c2.get_home_address() << endl;
    return 0;
}

从打印的信息中,我们可以知道:

创建 c2 对象时,执行了子类拷贝构造函数,具体构造顺序为:先调用父类默认构造函数,后调用子类拷贝构造函数。从而打印出 c2 home_address = 中国

三. 重写的子类拷贝构造函数时应当显式的告诉编译器去调用父类的拷贝构造函数去构造父类,从而避免调用子类拷贝构造函数去创建一个对象时,导致子类中包含父类的那部分数据丢失的情况发生。具体做法为:Child(const Child &other) : Parent(other);

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

class Parent {
public:
    Parent(string home_address = "中国") : m_home_address(home_address) {
        cout << "调用父类构造函数" << endl;
    }
    Parent(const Parent &other) {
        m_home_address = other.m_home_address;
        cout << "调用父类拷贝构造函数" << endl;
    }
    string get_home_address() {return m_home_address;}
private:
    string m_home_address;
};

class Child : public Parent {
public:
    Child(string home_address = "") : Parent(home_address) {
        cout << "调用子类构造函数" << endl;
    }
    Child(const Child &other) : Parent(other) {
        cout << "调用子类拷贝构造函数" << endl;
    }
private:
};

int main(int argc, char *argv[]) {
    Child c1("广州");
    cout << "c1 home_address = " << c1.get_home_address() << endl << endl;

    Child c2 = c1; // 调用了子类的默认拷贝构造函数
    cout << "c2 home_address = " << c2.get_home_address() << endl;
    return 0;
}

从打印的信息中,我们可以知道:

创建 c2 对象时,执行了子类拷贝构造函数,具体构造顺序为:先调用父类拷贝构造函数,后调用子类拷贝构造函数。从而打印出 c2 home_address = 广州

相关推荐
Yvemil7几秒前
MQ 架构设计原理与消息中间件详解(二)
开发语言·后端·ruby
程序员是干活的1 分钟前
私家车开车回家过节会发生什么事情
java·开发语言·软件构建·1024程序员节
Amor风信子14 分钟前
华为OD机试真题---跳房子II
java·数据结构·算法
我是陈泽16 分钟前
一行 Python 代码能实现什么丧心病狂的功能?圣诞树源代码
开发语言·python·程序员·编程·python教程·python学习·python教学
Mr.Z.41124 分钟前
【历年CSP-S复赛第一题】暴力解法与正解合集(2019-2022)
c++
优雅的小武先生27 分钟前
QT中的按钮控件和comboBox控件和spinBox控件无法点击的bug
开发语言·qt·bug
Death20028 分钟前
使用Qt进行TCP和UDP网络编程
网络·c++·qt·tcp/ip
戊子仲秋31 分钟前
【LeetCode】每日一题 2024_10_2 准时到达的列车最小时速(二分答案)
算法·leetcode·职场和发展
邓校长的编程课堂33 分钟前
助力信息学奥赛-VisuAlgo:提升编程与算法学习的可视化工具
学习·算法
虽千万人 吾往矣33 分钟前
golang gorm
开发语言·数据库·后端·tcp/ip·golang