本文介绍了基类的构造函数和析构函数是否与派生类一起调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为MyBase的类,它有一个构造函数和析构函数:

I have a class called MyBase which has a constructor and destructor:

class MyBase
{
public:
    MyBase(void);
    ~MyBase(void);
};

我有一个叫Banana的类,它扩展了MyBase:

and I have a class called Banana, that extends MyBase like so:

class Banana:public MyBase
{
public:
    Banana(void);
    ~Banana(void);
};

Banana中的新构造函数和析构函数的实现是否覆盖MyBase, ,并在Banana构造函数/析构函数执行之前或之后调用。

Does the implementation of the new constructor and destructor in Banana override the MyBase ones, or do they still exist, and get called say before or after the Banana constructor / destructor executes?

谢谢,如果我的问题看起来很蠢,我道歉。

Thanks, and my apologies if my question seems silly.

推荐答案

应该说

class Banana : public MyBase
{
public:
    Banana(void);
    ~Banana(void);
};

派生类的构造函数在基类的构造函数之后调用。析构函数以相反的顺序调用。

The constructor of the derived class gets called after the constructor of the base class. The destructors get called in reversed order.

这篇关于基类的构造函数和析构函数是否与派生类一起调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 03:49