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

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

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 = 广州

相关推荐
paterWang1 小时前
基于 Python 和 OpenCV 的酒店客房入侵检测系统设计与实现
开发语言·python·opencv
东方佑1 小时前
使用Python和OpenCV实现图像像素压缩与解压
开发语言·python·opencv
mit6.8242 小时前
[实现Rpc] 通信类抽象层 | function | using | 解耦合设计思想
c++·网络协议·rpc
我真不会起名字啊2 小时前
“深入浅出”系列之杂谈篇:(3)Qt5和Qt6该学哪个?
开发语言·qt
laimaxgg2 小时前
Qt常用控件之单选按钮QRadioButton
开发语言·c++·qt·ui·qt5
水瓶丫头站住2 小时前
Qt的QStackedWidget样式设置
开发语言·qt
尼尔森系4 小时前
排序与算法:希尔排序
c语言·算法·排序算法
小钊(求职中)4 小时前
Java开发实习面试笔试题(含答案)
java·开发语言·spring boot·spring·面试·tomcat·maven
AC使者4 小时前
A. C05.L08.贪心算法入门
算法·贪心算法
冠位观测者4 小时前
【Leetcode 每日一题】624. 数组列表中的最大距离
数据结构·算法·leetcode