从子类访问私有继承类的名称
Accessing the name of a private-inherited class from a subclass
以下代码在 g++ 中编译失败,使用 g++ 时出现此错误,使用 clang 时出现类似错误。
main.cpp:16:19: error: 'QByteArray' is a private member of 'QByteArray'
Container2(const QByteArray &ba);
^
main.cpp:3:18: note: constrained by private inheritance here
class Container1:private QByteArray
^~~~~~~~~~~~~~~~~~
../edge-clang/qt5/include/QtCore/qbytearray.h:167:21: note: member is declared here
class Q_CORE_EXPORT QByteArray
^
main.cpp:18:30: error: 'QByteArray' is a private member of 'QByteArray'
Container2::Container2(const QByteArray &ba):Container1(ba){}
^
main.cpp:3:18: note: constrained by private inheritance here
class Container1:private QByteArray
^~~~~~~~~~~~~~~~~~
../edge-clang/qt5/include/QtCore/qbytearray.h:167:21: note: member is declared here
class Q_CORE_EXPORT QByteArray
^
我不明白为什么不允许 Container2 继承 Container1 并通过公共构造函数初始化它,即使 QByteArray 的继承是私有的。
#include <QByteArray>
class Container1:private QByteArray
{
public:
Container1() {}
Container1(const QByteArray &ba);
};
Container1::Container1(const QByteArray &ba):QByteArray(ba){}
class Container2:public Container1
{
public:
Container2() {}
Container2(const QByteArray &ba);
};
Container2::Container2(const QByteArray &ba):Container1(ba){}
int main(int argc,char *argv[])
{
QByteArray ba;
Container1 c1(ba);
Container2 c2(ba);
}
这是一个很棒的错误!
在C
语境中,"A
"的意思是"我从我的基地得到的注入的类名A
"。
你可以把它想象成C::B::A
.
当然,由于私有继承,该基础无法访问:
class A {};
class B : private A {
public:
B() {}
B(const A&) {}
};
class C : public B {
public:
C(const A&) {}
};
int main()
{
A a;
B b(a);
C c(a);
}
// main.cpp:11:13: error: 'class A A::A' is inaccessible within this context
// C(const A&) {}
// ^
// main.cpp:1:9: note: declared here
// class A {};
// ^
(现场演示)
您可以通过将其称为 ::A
来解决此问题,采用更间接的路线来实际命名完全相同的类型:
class A {};
class B : private A {
public:
B() {}
B(const A&) {}
};
class C : public B {
public:
C(const ::A&) {}
};
int main()
{
A a;
B b(a);
C c(a);
}
// OK
(现场演示)
顺便说一句,完全相同的逻辑适用于private
继承的成员变量:
int x = 1;
class A
{
private:
int x = 2;
};
class B : A {
public:
B() { int y = x; }
};
int main()
{
A a;
B b(a);
}
// main.cpp: In constructor 'B::B()':
// main.cpp:11:17: error: 'int A::x' is private within this context
// B() { int y = x; }
(现场演示)
从表面上看,这一切看起来确实很愚蠢,但可能有一个很好的理由。
在您的情况下,那么:
Container2(const ::QByteArray &ba);
相关文章:
- 继承期间显示未知行为的子类
- C++ 继承:将子类传递给需要基类的函数并获取子类行为
- 为什么我的子类不继承父类的字符串?
- 初始化依赖于子类的继承类的常量类成员
- 当子类需要在 c++ 中相互包含时,继承有缺陷
- 无法将数据从父类继承到C++中的子类
- C++子类继承
- C++继承,如何在基类的方法中调用子类的方法?
- 如果不手动完成,子类是否继承父类的析构函数?
- 模板化类继承的子类定义中的错误
- 模板类如何在 GCC 和 Clang 中继承子类
- C 模板继承.子类应用固定类型替换基类中的类型
- 具有菱形继承结构的子类的大小似乎很奇怪
- C++继承子类可修改变量
- 未调用/继承子类中的移动赋值运算符
- 如何从继承子类访问基类中的私有成员(c++)
- 如果某些实例不使用基类的属性,我应该继承子类吗?
- 从其他子类继承子类
- c++继承:子类实例同时使用子和父构造函数
- 继承子类时的默认保护级别是什么