如果不是手动完成,children 类 会继承 parent 的析构函数吗?

Do children classes inherit parent's Destructors if not done manually?

如果给定一个包含已定义析构函数的 parent class A,将 B-it 的 child- 在创建 B object 时执行它?即使我没有手动创建也没有在 B class?

中继承 A 的析构函数

我希望它会,因为我在 c++ 视频教程中看到它,但不明白为什么?因为Constructor是手动继承的!

编译器总是为您的 class 实现一个析构函数,如果您还没有声明的话。编译器还会生成调用基本 class 析构函数的代码。

换句话说,基础 class 析构函数不会被继承,而是在派生的 class 析构函数之后被调用。

参见Destructor

Destruction sequence

For both user-defined or implicitly-defined destructors, after the body of the destructor is executed, the compiler calls the destructors for all non-static non-variant members of the class, in reverse order of declaration, then it calls the destructors of all direct non-virtual base classes in reverse order of construction (which in turn call the destructors of their members and their base classes, etc), and then, if this object is of most-derived class, it calls the destructors of all virtual bases.

Even when the destructor is called directly (e.g. obj.~Foo();), the return statement in ~Foo() does not return control to the caller immediately: it calls all those member and base destructors first.

子class继承了父class的数据成员和成员函数,但是私有成员在子class.Only中仍然不可访问Public成员函数和数据成员是可访问的in child class.In Inheritance constructor or destructor is not inherited by child class but in the child class constructor and destructor are an explicit call to base class 构造函数和析构函数。 每当我们创建 child class 的对象时,首先将调用 base class 的构造函数然后派生 class 的构造函数是 called.while 如果 child class 的对象被销毁子 class 的析构函数将首先被调用,然后父 class 的析构函数将被调用。