部分隐藏的继承树

Partially hidden inheritance tree

本文关键字:继承 隐藏      更新时间:2023-10-16

我有一个这样的类树:

class A;
class B : public A;

然后我想创建一个从类 B 派生的类。但是我希望该派生对外部成员以及从C类继承的任何其他人隐藏

class C : private B;
void test() {
    C c;
    B *b = &c; // compiler error: B in C is private and is therefore not type compatible. This is desired.
}

但是,我也想揭示类 A 的继承.在这种情况下隐藏类 B 也会隐藏类 A。

void test2() {
    C c;
    A *a = &c; // Compiler error: A is only accessible through B which is not possible with a private inheritance of B. This is not desired; this conversion should be possible.
}

我可以再次从 A 继承,但如果 A 有的话,这显然会创建重复的成员变量。我可以创建类 A 的虚拟继承,但是我认为它不会产生我想要的确切效果,因为这会影响整个树而不是这一段(对吧?

我想显而易见的解决方案是创建一个类型转换成员函数:

class C : private B {
    A * turn_into_A() {
        // Since B is an A and we are still in the scope of C, this will succeed
        return this;
    }
};

但是,我宁愿避免显式类型转换,例如这种情况,

任何理智的人都可能会告诉我我做错了。他们可能是对的。但是我只是为了知识而想知道:有没有办法在没有虚拟继承或显式成员函数的类型转换的情况下做到这一点?

我找到了一个可行的解决方案:

class A {
public:
    void somethingA() {
        std::cout << "a" << std::endl;
        return;
    }
 };
class B :
    public A {
public:
    void somethingB() {
        std::cout << "b" << std::endl;
        return;
    }
};
class C :
    private B {
public:
using B::A; // While B is private (and hidden), this exposes access to B::A
    void somethingC() {
        std::cout << "c" << std::endl;
        return;
    }
};
int main(int argc, char **argv) {
    C c;
    B* b = &c; // Compiler error: cannot convert because B is private (desired)
    A* a = &c; // Okay! (required)
    c.somethingC();
    c.somethingB(); // Compiler error: private. (desired)
    c.somethingA(); // Compiler error: A is exposed, but not A's members. This can be solved by adding 'using B::A::somethingA()' in class declaration (undesired but acceptable in my situation)
    a->somethingA(); // Okay! (of course)
}

它并不完美,因为它只公开 C 以便能够转换为 A(就我而言,无论如何我最终都会这样做,所以这很好)。但是,它不会直接公开 A 的成员以允许 C 用作 A,例如,除非您特别公开 B::A::somethingA,否则您不能调用 c::somethingA()。

继承描述了

IS-A 关系。因此,在对象模型中,B IS-A A,C IS-A B。那么,你为什么不使用

class C : public B { ...};

这样您就可以根据需要将 C 对象视为 B 对象以及 A 对象。希望有帮助。