公开受保护的内部阶级 [英] Make protected inner class public

查看:60
本文介绍了公开受保护的内部阶级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程:

class A
{
protected:
    class InnerA {};
};

现在,我需要在测试中访问InnerA类. 从理论上讲,我可以创建一个派生自A的测试类,并将A类的某些部分公开.我当然可以使用字段来做到这一点,但是不能使用内部类来做到这一点.

Now I need to gain access to InnerA class in the tests. Theoretically I could create a test class derived from A and make some parts of A class public. I could do this with fields of course, but not with inner classes.

是否有办法从A类层次结构(所有派生自A的类)的外部查看受保护的InnerA类?我希望能够写:

Is there a way to see the protected InnerA class from outside of A class hierarchy (all classes derived from A)? I would like to be able to write:

Test(MyTest, InnerATest)
{
    TestA::InnerA x;
    x.DoSth();
}

推荐答案

事实上,您可以继承和公开该类.

You can inherit and publicize the class, as a matter of fact.

class A
{
protected:
    class InnerA {};
};

struct expose : A {
    using A::InnerA;
};

int main() {

    expose::InnerA ia;

    return 0;
}

与其他任何受保护成员一样.这是标准的C ++.

As you would any other protected member of the base. This is standard C++.

这篇关于公开受保护的内部阶级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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