Search code examples
c++googletest

Make protected inner class public


I have the following class:

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

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.

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();
}

Solution

  • 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;
    }
    

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