C ++ 11继承构造函数和访问修饰符 [英] C++11 inheriting constructors and access modifiers

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

问题描述

假设有以下布局:

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;
};

我应该能够指定 Derived 的构造函数作为public在这里? VC ++给出以下错误:

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


无法访问在'Derived'类中声明的受保护成员

编译器生成了'Derived :: Derived'here [指向使用Base :: Base
行]

查看Derived的声明

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'

ie它忽略了在继承的构造函数上面的访问修饰符。

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

这是功能的限制吗?对于 Base 类没有任何意义,有一个公共构造函数,因为它永远不能直接实例化(由于纯虚方法)。

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).

推荐答案

根据12.9 / 4,继承构造函数,当使用X :: X ,

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


这样声明的构造函数具有与X中相应构造函数相同的访问权限。

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

所以继承的构造函数也是 protected

So the inherited constructor is also protected.

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

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