C ++构造函数/析构函数继承 [英] C++ Constructor/Destructor inheritance

查看:158
本文介绍了C ++构造函数/析构函数继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EDIT:答案摘要



在下文中,B是A的子类。



这是一个术语问题; ctors和dtors是继承的,因为B的ctor / dtor将不会从A的接口借用。




  • 构造函数

    • B不从A继承构造函数;

    • 除非B的ctor显式地调用 A的ctor之一,默认的ctor从A将在 B的ctor主体之前自动调用

  • $ b $ b
  • 破坏者

    • B不继承A的dtor;

    • 退出,B的析构函数将自动调用A的析构函数。



致谢:
我要特别感谢Oli Charlesworth和Kos的回答,我把Kos的回答设为解决方案,因为它是我最了解的。






ORIGINAL POST



当您搜索C ++ destructor继承网站:stackoverflow.com,您当前会发现以下帖子:


  1. 构造函数和析构函数继承:具有30k +信誉的两个用户表示它是继承的,不是

  2. 虚拟析构函数是否继承?:这里没有提到任何指向未继承的析构函数。

  3. C ++中的析构函数和继承?似乎表明析构器是继承的。

Q1:我从实践中也知道,在没有为派生类显式定义构造函数的情况下,使用与其父构造函数相同的原型初始化派生对象,是否正确?






尽管从帖子中可以清楚地看到析构函数似乎是继承的,但我仍然感到困惑的是,拥有32k名誉的用户会说它不是。我写了一个例子,应该澄清每个人的想法:

  #include< cstdio& 

/ ****************************** /

//基类
struct A
{
A(){printf(\tInstance counter =%d(ctor)\\\
,++ instance_counter); }
〜A(){printf(\tInstance counter =%d(dtor)\\\
,--instance_counter); }

static int instance_counter;
};

//默认为ctor / dtor的继承类
class B:public A {};

//具有定义的ctor / dtor的继承类
struct C:public A
{
C(){printf(\tC said hi!\ n); }
〜C(){printf(\tC says bye!\\\
); }
};

/ ****************************** /

//初始化计数器
int A :: instance_counter = 0;

/ ****************************** /

//一些测试
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\\\
);删除a_ptr

printf(Create C \\\
); C c;
printf(Delete C \\\
); c。〜C();

}

这里是输出):

 创建A 
实例计数器= 1(ctor)
删除A
实例计数器= 0(dtor)
创建B
实例计数器= 1(ctor)
删除B
实例计数器= 0(dtor)
创建新的B *
实例计数器= 1(ctor)
删除上一个指针
实例计数器= 0(dtor)
创建C
实例计数器= 1 b C说hi!
删除C
C说再见!
实例计数器= 0(dtor)//我们退出main()现在
C说再见!
实例计数器= -1(dtor)
实例计数器= -2(dtor)
实例计数器= -3(dtor)

Q2:任何认为不是继承的人,请解释一下。



Q3:那么,当你使用输入调用子类的构造函数时会发生什么?是否调用超类的空构造函数?

解决方案

术语,术语...



好的,Foo是继承的是什么意思?我们的意思是如果 A 的对象在其接口中有 Foo ,那么 A 的子类在其接口中也有 Foo >


  • 构造函数不是对象界面的一部分。它们直接属于类。类 A B 可能提供完全不同的构造函数集。

    实施细节:每个B的构造函数调用一些A的构造函数。
  • Destructors 确实是每个对象接口的一部分,因为对象的用户负责调用它们(即直接使用 delete 或间接通过让对象超出范围)。 每个对象都有一个析构函数:它自己的析构函数,它可以是虚拟函数。



    (实现细节:B的析构函数调用A的析构函数。) li>



所以:基础和派生构造函数和析构函数之间有一个连接,但它不像它们是继承的。



我希望这能回答你的想法。


EDIT : Summary of answers

In the following, B is a subclass of 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.

  • 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).
  • Destructors:
    • B does not inherit A's dtor;
    • After it exits, B's destructor will automatically call A's destructor.

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.


ORIGINAL POST

When you search for "C++ destructor inheritance site:stackoverflow.com" on Google, you currently find the following posts:

  1. Constructor and Destructor Inheritance: two users with 30k+ reputation say that it is inherited, and that it's not
  2. Are virtual destructors inherited?: here nothing is mentioned that would point to destructors not being inherited
  3. Destructors and inheritance in C++?: The comments seem to indicate the destructors are inherited

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?


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( "\tInstance counter = %d (ctor)\n", ++instance_counter ); }
    ~A() { printf( "\tInstance counter = %d (dtor)\n", --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("\tC says hi!\n"); }
    ~C() { printf("\tC says bye!\n"); }
};

/******************************/

// Initialize counter
int A::instance_counter = 0;

/******************************/

// A few tests
int main()
{
    printf("Create A\n"); A a;
    printf("Delete A\n"); a.~A();

    printf("Create B\n"); B b;
    printf("Delete B\n"); b.~B();

    printf("Create new B stored as A*\n"); A *a_ptr = new B();
    printf("Delete previous pointer\n"); delete a_ptr;

    printf("Create C\n"); C c;
    printf("Delete C\n"); c.~C();

}

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)

Q2: Can anybody who thinks it's not inherited please explain that?

Q3: So what happens when you call the constructor of a subclass with inputs? Is the "empty constructor" of the superclass called as well?

解决方案

Terminology, terminology...

OK, what do we mean by "Foo is inherited"? We mean that if objects of class A have Foo in its interface, then objects of class B which is a subclass of A also have Foo in its interface.

  • Constructors aren't a part of objects' interface. They belong directly to classes. Classes A and B may provide completely different sets of constructors. No "being inherited" here.

    (Implementation detail: each B's constructors calls some A's constructor.)

  • 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.

    (Implementation detail: B's destructor calls A's destructor.)

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.

这篇关于C ++构造函数/析构函数继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆