构造函数和析构函数继承 [英] Constructor and Destructor Inheritance

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

问题描述

我相信构造函数 基类中的析构函数 不能由基类的派生类继承。我的理解是否正确。

I believe Constructors and Destructors in base class cannot be inherited by derived classes of the base class. Is my understanding correct.

推荐答案

您的理解是正确的。例如,如果你有

Your understanding is correct. For example, if you have

class Base
{
  Base(int i) {}
};

class Derived: public Base {};

Derived d(3);

这将无法编译,因为Base构造函数未被继承。
请注意,如果可能,编译器会创建默认和复制构造函数,并调用相应的基类构造函数,因此对于那些构造函数,它看起来像,就好像那些是继承的。

This will not compile because the Base constructor is not inherited. Note that default and copy constructor are created by the compiler if possible, and call the corresponding constructor of base classes, therefore for those constructors it looks as if those were inherited.

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

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