C++父类与子类间构造,析构,拷贝的调用关系

13 篇文章 0 订阅

C++父类与子类间构造,析构,拷贝的调用关系

构造

子类的构造在执行它的构造函数前会根据继承表的顺序执行父类的构造函数

  • 默认执行无参构造
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;

class Base
{
    char *str;

public:
    Base(void)
    {
        cout << "父类无参构造" << endl;
    }
    Base(const char *str)
    {
        this->str = new char[strlen(str) + 1];
        cout << "父类有参构造" << str << endl;
    }
};

class A : public Base
{
public:
    A(void)
    {
        cout << "子类无参构造" << endl;
    }
    A(const char *str)// : Base(str)
    {
        cout << "子类有参构造" << str << endl;
    }
};

int main()
{
    A a("h");
}

运行结果:

wcq@DESKTOP-PL6DIJT:~/code$ cd "/home/wcq/code/" && g++ test.cpp -lpthread -o test.out && ./test.out
父类无参构造
子类有参构造h
  • 显式调用有参构造,在子类的构造函数后,初始化列表中显式调用父类的有参构造函数
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;

class Base
{
    char *str;

public:
    Base(void)
    {
        cout << "父类无参构造" << endl;
    }
    Base(const char *str)
    {
        this->str = new char[strlen(str) + 1];
        cout << "父类有参构造" << str << endl;
    }
};

class A : public Base
{
public:
    A(void)
    {
        cout << "子类无参构造" << endl;
    }
    A(const char *str) : Base(str)
    {
        cout << "子类有参构造" << str << endl;
    }
};

int main()
{
    A a("h");
}

运行结果

wcq@DESKTOP-PL6DIJT:~/code$ cd "/home/wcq/code/" && g++ test.cpp -lpthread -o test.out && ./test.out
父类有参构造h
子类有参构造h

析构

子类在它的析构函数执行完后,会根据继承表的顺序逆序执行父类的析构函数

  • 注意: 父类的指针可以指向子类对象,当通过父类指针释放对象时,只会调用父类的析构函数,而这种析构方式有可能造成内存泄漏
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;

class Base
{
    char *str;

public:
    Base(void)
    {
        cout << "父类无参构造" << endl;
    }
    Base(const char *str)
    {
        this->str = new char[strlen(str) + 1];
        cout << "父类有参构造" << str << endl;
    }
    ~Base(void)
    {
        cout << "父类析构" << endl;
    }
};

class A : public Base
{
public:
    A(void)
    {
        cout << "子类无参构造" << endl;
    }
    A(const char *str) : Base(str)
    {
        cout << "子类有参构造" << str << endl;
    }
    ~A(void)
    {
        cout << "子类析构" << endl;
    }
};

int main()
{
    A *a = new A("xixi");
    delete a;
    cout << "----------------" << endl;
    Base *b = new A("haha");
    delete b;
}

运行结果

wcq@DESKTOP-PL6DIJT:~/code$ cd "/home/wcq/code/" && g++ test.cpp -lpthread -o test.out && ./test.out
父类有参构造xixi
子类有参构造xixi
子类析构
父类析构
----------------
父类有参构造haha
子类有参构造haha
父类析构

拷贝构造
当使用子类对象来初始化新的子类对象时,会自动调用缺省的拷贝构造函数,并且会调用父类缺省的拷贝构造函数

  • 如果子类中实现了拷贝构造,需要显式调用父类拷贝构造,否则就会调用无参构造
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;

class Base
{
public:
    char *str;

    Base(void)
    {
        cout << "父类无参构造" << endl;
    }
    Base(const char *str)
    {
        this->str = new char[strlen(str) + 1];
        cout << "父类有参构造" << str << endl;
    }
    Base(Base &that)
    {
        cout << "父类拷贝构造" << endl;
    }
};

class A : public Base
{
public:
    A(void)
    {
        cout << "子类无参构造" << endl;
    }
    A(const char *str) : Base(str)
    {
        cout << "子类有参构造" << str << endl;
    }
    A(A &that) : Base(that)
    {
        cout << "子类拷贝构造" << endl;
    }
};

int main()
{
    A a("x");
    cout << "------" << endl;
    A a1 = a;
    cout << "------" << a.str << endl;
}

运行结果(子类中不重载拷贝构造)

wcq@DESKTOP-PL6DIJT:~/code$ cd "/home/wcq/code/" && g++ test.cpp -lpthread -o test.out && ./test.out
父类有参构造x
子类有参构造x
------
父类拷贝构造
------

运行结果(子类中重载拷贝构造但不显式调用父类拷贝构造)

wcq@DESKTOP-PL6DIJT:~/code$ cd "/home/wcq/code/" && g++ test.cpp -lpthread -o test.out && ./test.out
父类有参构造x
子类有参构造x
------
父类无参构造
子类拷贝构造
------

运行结果(子类中重载且显式调用父类拷贝构造)

wcq@DESKTOP-PL6DIJT:~/code$ cd "/home/wcq/code/" && g++ test.cpp -lpthread -o test.out && ./test.out
父类有参构造x
子类有参构造x
------
父类拷贝构造
子类拷贝构造
------
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,子类拷贝构造函数默认不会自动调用父类拷贝构造函数来拷贝父类特有成员。这种认为是错误的。子类拷贝构造函数只会负责拷贝子类特有的成员,而不会主动调用父类拷贝构造函数来拷贝父类特有成员。 如果想要在子类拷贝构造函数中拷贝父类特有成员,需要手动调用父类拷贝构造函数,并将父类对象作为参数传递给它。这样才能确保父类特有成员也被正确地拷贝子类对象中。 例如,假设有一个派生类Derived,它继承自基类Base,并且Derived类拥有一些父类没有的特有成员。那么,在Derived类的拷贝构造函数中,应该在成员初始化列表中调用Base类的拷贝构造函数来拷贝父类特有成员。这样才能保证Derived对象的父类特有成员得到正确的拷贝。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [子类拷贝构造是否会调用父类拷贝构造?](https://blog.csdn.net/Think88666/article/details/91639448)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C++ 子类拷贝构造函数调用父类拷贝构造函数](https://blog.csdn.net/weixin_34392906/article/details/92771176)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值