gpt4 book ai didi

c++ - 使用声明指定的构造函数

转载 作者:行者123 更新时间:2023-11-27 23:05:07 27 4
gpt4 key购买 nike

我正在尝试声明一个成员名称,它是基类的构造函数,如下所示:

#include <iostream>

class A{ };

class B: public A
{
using A::A; //error: ‘A::A’ names constructor
};

int main()
{
}

哪里规定不能通过using声明接受constructor?我正在寻找来自标准的相应报价。

最佳答案

Where is it specified that constructor cannot be accepted by using declaration?

无处可去,因为它可以。请参阅12 个特殊成员函数:

12.9 Inheriting constructors [class.inhctor]

A using-declaration (7.3.3) 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.

....

这是一个例子:

struct A
{
explicit A(int) {}
};

struct B: A
{
using A::A;
};

int main()
{
B b{42};
}

关于c++ - 使用声明指定的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24165504/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com