C++ 构造函数/析构函数继承
答案摘要
下面,B是A的子类.
这是一个术语问题;ctors 和 dtors 不 继承,从某种意义上说 B 的 ctor/dtor 不会从 A 的接口中借用.一个类至少有一个构造函数,并且正好有一个析构函数.
It's a matter of terminology; ctors and dtors are not inherited, in the sense that the ctor/dtor of B will not be borrowed from A's interface. A class has at least one constructor, and has exactly one destructor.
- 构造函数:
- B 没有从 A 继承构造函数;
- 除非 B 的 ctor 明确调用 A 的 ctor 之一,否则 A 的默认 ctor 将在 B 的 ctor 主体之前自动调用(这个想法是 A 需要初始化在 B 被创建之前).
- Constructors:
- B does not inherit constructors from A;
- Unless B's ctor explicitely calls one of A's ctor, the default ctor from A will be called automatically before B's ctor body (the idea being that A needs to be initialized before B gets created).
- B 没有继承 A 的 dtor;
- 退出后,B的析构函数会自动调用A的析构函数.
- B does not inherit A's dtor;
- After it exits, B's destructor will automatically call A's destructor.
致谢:我要特别感谢 Oli Charlesworth 和 Kos 的回答,我将 Kos 的答案设置为解决方案,因为它是我最理解的.
Acknowledgements: I would like to thank especially Oli Charlesworth and Kos for their answers, I set Kos' answer as the solution because it was the one I understood best.
原帖
当您在 Google 上搜索C++ 析构函数继承站点:stackoverflow.com"时,您当前会找到以下帖子:
When you search for "C++ destructor inheritance site:stackoverflow.com" on Google, you currently find the following posts:
- 构造函数和析构函数继承:两个30k+声望的用户说是继承的,并且它不是
- 虚拟析构函数是否被继承?:这里没有提到任何指向析构函数不被继承的内容继承
- C++ 中的析构函数和继承?:评论似乎表明析构函数是继承
- Constructor and Destructor Inheritance: two users with 30k+ reputation say that it is inherited, and that it's not
- Are virtual destructors inherited?: here nothing is mentioned that would point to destructors not being inherited
- Destructors and inheritance in C++?: The comments seem to indicate the destructors are inherited
问题 1: 我还从实践中了解到,如果没有为派生类明确定义构造函数,则不能使用与其父构造函数相同的原型初始化派生对象,对吗?
Q1: What I also know from practice, is that you cannot initialize a derived object with the same prototype than it's parent constructor without explicitely defining a constructor for the derived class, is that correct?
尽管从帖子中很清楚析构函数似乎是继承的,但我仍然对拥有 32k 声誉的用户会说不是这样的事实感到困惑.我写了一个小例子,应该可以澄清每个人的想法:
Even though it's rather clear from the posts that destructors seem to be inherited, I'm still puzzled by the fact that a user with 32k reputation would say its not. I wrote a little example that should clarify everyone's mind:
#include <cstdio> /******************************/ // Base class struct A { A() { printf( " Instance counter = %d (ctor) ", ++instance_counter ); } ~A() { printf( " Instance counter = %d (dtor) ", --instance_counter ); } static int instance_counter; }; // Inherited class with default ctor/dtor class B : public A {}; // Inherited class with defined ctor/dtor struct C : public A { C() { printf(" C says hi! "); } ~C() { printf(" C says bye! "); } }; /******************************/ // Initialize counter int A::instance_counter = 0; /******************************/ // A few tests int main() { printf("Create A "); A a; printf("Delete A "); a.~A(); printf("Create B "); B b; printf("Delete B "); b.~B(); printf("Create new B stored as A* "); A *a_ptr = new B(); printf("Delete previous pointer "); delete a_ptr; printf("Create C "); C c; printf("Delete C "); c.~C(); }
这是输出(使用 g++ 4.4.3 编译):
and here is the output (compiled with g++ 4.4.3):
Create A Instance counter = 1 (ctor) Delete A Instance counter = 0 (dtor) Create B Instance counter = 1 (ctor) Delete B Instance counter = 0 (dtor) Create new B stored as A* Instance counter = 1 (ctor) Delete previous pointer Instance counter = 0 (dtor) Create C Instance counter = 1 (ctor) C says hi! Delete C C says bye! Instance counter = 0 (dtor) // We exit main() now C says bye! Instance counter = -1 (dtor) Instance counter = -2 (dtor) Instance counter = -3 (dtor)
问题 2: 任何认为它不是遗传的人能解释一下吗?
Q2: Can anybody who thinks it's not inherited please explain that?
问题 3: 那么当您使用输入调用子类的构造函数时会发生什么?超类的空构造函数"也被调用了吗?
Q3: So what happens when you call the constructor of a subclass with inputs? Is the "empty constructor" of the superclass called as well?
推荐答案
术语,术语...
好的,我们所说的Foo 是继承的"是什么意思?我们的意思是,如果
A
类的对象在其接口中有Foo
,那么B
类的对象是A<的子类/code> 在其界面中也有
Foo
.OK, what do we mean by "Foo is inherited"? We mean that if objects of class
A
haveFoo
in its interface, then objects of classB
which is a subclass ofA
also haveFoo
in its interface.构造函数不是对象接口的一部分.它们直接属于类.类
A
和B
可能提供完全不同的构造函数集.这里没有被继承".
Constructors aren't a part of objects' interface. They belong directly to classes. Classes
A
andB
may provide completely different sets of constructors. No "being inherited" here.
(实现细节:每个 B 的构造函数调用一些 A 的构造函数.)
析构函数确实是每个对象接口的一部分,因为对象的用户负责调用它们(即直接使用
delete
或通过让对象间接超出范围).每个对象都有一个析构函数:它自己的析构函数,它可能是一个虚拟的析构函数.它始终是它自己的,而不是继承的.Destructors indeed are a part of each object's interface, since the object's user is responsible for calling them (i.e. directly with
delete
or indirectly by letting an object out of scope). Each object has exactly one destructor: its own destructor, which might optionally be a virtual one. It is always its own, and it's not inherited.(实现细节:B 的析构函数调用 A 的析构函数.)
所以:基类和派生的构造函数和析构函数之间存在联系,但这不像它们是继承的".
So: there's a connection between base and derived constructors and destructors, but it's not like "they're inherited".
我希望这能回答你的想法.
I hope this answers what you have in mind.
相关文章