C++ 什么是构造函数继承?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9979194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:28:37  来源:igfitidea点击:

What is constructor inheritance?

c++inheritanceconstructorc++11

提问by badmaash

In C++11, what is meant by inheriting the constructor? If it is what i think it is (Base class constructor is brought in the scope of the derived class), what are its implications on my code? What are the applications of such a feature?

在 C++11 中,继承构造函数是什么意思?如果它是我认为的那样(基类构造函数被引入派生类的范围内),它对我的​​代码有什么影响?这种功能有哪些应用?

回答by Andrew Tomazos

Inheriting Constructors means just that. A derived class can implicitly inherit constructors from its base class(es).

继承构造函数就是这个意思。派生类可以从其基类隐式继承构造函数。

The syntax is as follows:

语法如下:

struct B
{
    B(int); // normal constructor 1
    B(string); // normal constructor 2
};

struct D : B
{
    using B::B; // inherit constructors from B
};

So now D has the following constructors implicitly defined:

所以现在 D 隐式定义了以下构造函数:

D::D(int); // inherited
D::D(string); // inherited

Ds members are default constructed by these inherited constructors.

Ds 成员默认由这些继承的构造函数构造。

It is as though the constructors were defined as follows:

就好像构造函数定义如下:

D::D(int x) : B(x) {}
D::D(string s) : B(s) {}

The feature isn't anything special. It is just a shorthand to save typing boilerplate code.

该功能没有什么特别之处。这只是保存输入样板代码的速记。

Here are the gory details:

以下是血腥细节:

12.9 Inheriting Constructors

1) A using-declaration that names a constructor implicitly declares a set of inheriting constructors. The candidate set of inherited constructors from the class X named in the using-declaration consists of actual constructors and notional constructors that result from the transformation of defaulted parameters as follows:

  • all non-template constructors of X, and
  • for each non-template constructor of X that has at least one parameter with a default argument, the set of constructors that results from omitting any ellipsis parameter specification and successively omitting parameters with a default argument from the end of the parameter-type-list, and
  • all constructor templates of X, and
  • for each constructor template of X that has at least one parameter with a default argument, the set of constructor templates that results from omitting any ellipsis parameter specification and successively omitting parameters with a default argument from the end of the parameter-type-list

12.9 继承构造函数

1) 命名构造函数的 using 声明隐式声明了一组继承构造函数。来自 using 声明中命名的类 X 的继承构造函数的候选集由实际构造函数和由默认参数转换产生的名义构造函数组成,如下所示:

  • X 的所有非模板构造函数,以及
  • 对于 X 的每个具有至少一个带默认参数的参数的非模板构造函数,从参数类型列表的末尾省略任何省略号参数规范并连续省略带有默认参数的参数所产生的一组构造函数,和
  • X 的所有构造函数模板,以及
  • 对于具有至少一个带默认参数的参数的 X 的每个构造函数模板,从参数类型列表的末尾省略任何省略号参数规范并连续省略带有默认参数的参数所产生的一组构造函数模板