简体   繁体   English

C ++:如何从封闭的类继承

[英]C++: How to inherit from enclosed class

I would like to have a class inherit from its enclosed class, as in: 我想从其封闭的类继承一个类,如下所示:

class A : public A::B {
public:
    class B {};
};

However, the compiler complains that A::B is not defined: 但是,编译器抱怨没有定义A::B

error: expected class-name before '{' token
 class A : public A::B {

That is, A::B won't be usable until the definition of A is complete. 也就是说, A::B将无法使用,直到定义A是完整的。

I have tried to preface the above with 我试图以上述开头

class A;
class A::B;

But it doesn't help. 但这没有帮助。 How can I get this declared and defined correctly? 如何获得正确的声明和定义?

Note: Essentially, I am trying to do the opposite of this question . 注意:本质上,我正在尝试与此问题相反。

这是不可能的,没有定义外部类就无法声明内部类。

At the point where you specify the inheritance, the inner class is not yet know. 在指定继承的位置,尚不知道内部类。 Thus you can't do that. 因此,您无法做到这一点。 But if the point is to limit the scope, then just use a namespace, eg call it detail or implementation . 但是,如果要限制范围,则只需使用名称空间,例如将其称为detailimplementation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM