【问题标题】:Why can't an inherited protected constructor be made public?为什么不能将继承的受保护构造函数公开?
【发布时间】:2020-09-17 19:05:02
【问题描述】:

考虑:

class A
{
protected:
    A(int) {}
    void f(int) {}

public:
    A() {}
};

class B : public A
{
public:
    using A::A;
    using A::f;
};

int main()
{
    B().f(1); // ok
    B(1); // error: 'A::A(int)' is protected within this context
}

为什么不能将继承的protected构造函数设为public,而继承的protected成员函数可以?

【问题讨论】:

    标签: c++ oop c++11 inheritance constructor


    【解决方案1】:

    实际上,继承的构造函数可以公开,但不仅仅是您编写的方式。您可以按如下方式定义您的 B 类:

    class B : public A {
    public:
        B() {}
    
        B(int x) : A(x) {}  // instead of using A::A(int)
        using A::f;
    };
    

    (见GodBolt

    也许标准委员会认为说using A::A 会有点模棱两可,因为基类的构造函数与子类的构造函数并不完全相同。

    【讨论】:

      【解决方案2】:

      与其他成员不同,引入 inherited constructor 的 using 声明的可访问性被忽略。

      [namespace.udecl]/19,

      (强调我的)

      using-declaration 创建的同义词具有member-declaration 的通常可访问性。命名构造函数的using-declarator 不会创建同义词;相反,如果附加的构造函数在用于构造相应基类的对象时可访问,那么它们是可访问的,并且 using-declaration 的可访问性被忽略。

      【讨论】:

        猜你喜欢
        • 2019-08-24
        • 2016-04-07
        • 2011-04-09
        • 1970-01-01
        • 2010-09-17
        • 2013-08-01
        • 1970-01-01
        • 2017-03-11
        • 2010-10-20
        相关资源
        最近更新 更多