C++11 inheriting constructors and access modifiers

爷,独闯天下 提交于 2019-12-17 06:11:36

问题


Assuming the following layout:

class Base
{
protected:
    Base(P1 p1, P2 p2, P3 p3);

public:
    virtual void SomeMethod() = 0;
}

class Derived : public Base
{
public:
    using Base::Base;

public:
    virtual void SomeMethod() override;
};

Should I be able to specify Derived's constructor as public here? VC++ gives the following error:

cannot access protected member declared in class 'Derived'
compiler has generated 'Derived::Derived' here [points to the using Base::Base line]
see declaration of 'Derived'

i.e. it's ignoring the access modifier above the inherited constructor.

Is this a limitation of the feature? It doesn't make any sense for the Base class to have a public constructor, as it can never be instantiated directly (due to the pure virtual method).


回答1:


According to 12.9/4, "Inheriting constructors", when saying using X::X,

A constructor so declared has the same access as the corresponding constructor in X.

So the inherited constructor is also protected.



来源:https://stackoverflow.com/questions/21015909/c11-inheriting-constructors-and-access-modifiers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!