二级子类中不可访问的基
Inaccessible base in second level descendant class
为什么我不能成为Grand类型的类成员?
class Grand
{};
class Father :private Grand
{};
class Son :public Father
{
Grand g; //This gives error. Class Grand is Inaccessible.
};
因为它自动在函数之前添加父类"spacename",并且Father::Grand::Grand()(构造函数)是私有
class Grand
{};
class Father :private Grand
{};
class Son :public Father
{
::Grand g;
};
之所以有效,是因为直接使用Grand类,而不是继承
这修复了它:
class Grand
{};
class Father :private Grand
{};
typedef Grand MyGrand;
class Son :public Father
{
MyGrand g; // This now compiles successfully
};
问题是,像private这样的说明符会影响名称查找中的可访问性,但不会影响可见性。因此,当您说Grand g
时,编译器首先遍历继承树,以防您最终解决类似Grand::MyType
的问题。
这是因为您的Grand
被继承private
的Father
隐藏了,请尝试使用全局作用域:
class Son : public Father {
::Grand g;
};
相关文章:
- 通过指向指针数组的指针访问子类的属性
- 如何访问子类的新成员函数?
- 从父类的向量访问子类函数,而无需向下转换
- 从C++中的父类实例访问子类方法
- 如何在 getter 的父类中初始化变量的情况下访问子类中的变量
- 如何从父类访问子类
- cpp 从需要超类对象的函数访问子类对象方法
- 从超类访问子类
- C++ 访问子类中的数据
- 我调用的函数不是通过多态访问子类的函数
- 带模板的继承(访问子类成员函数内父类的变量和对象)
- 从父类访问子类属性
- 可视化 当基类和子类具有相同的对象名称但类型不同时,如何访问子类的数据成员。在C++
- 如何访问子类成员
- 当子对象引用被分配给父对象指针时,为什么不能访问子类
- 使用指向无法dynamic_cast的基 abstact 类的指针访问子类成员
- 在c++中从超类数组中访问子类方法
- 通过基类指针访问子类的成员函数
- 在c++中通过父类访问子类的元素
- 从父类访问子类