C++11 inheriting constructors and access modifiers(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"[指向使用 Base::Base行]
见派生"的声明
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.
这是功能的限制吗?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,继承构造函数",当说using 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 继承构造函数和访问修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++11 继承构造函数和访问修饰符
基础教程推荐
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01