派生自私有范围内声明的类

时间:2016-04-05 12:14:43

标签: c++ c++11 inheritance inner-classes

我目前正在重构一些遗留代码,并希望将多个if ... elseif ...语句分解为一系列实现各种策略的类。

由于我必须访问原始对象的内部,我将把新类声明为嵌套类;因为外部世界没有人应该访问它们,我宁愿在私人范围内宣布它们。

为了尽可能少地公开实现细节,我想知道是否可以只在头文件中转发声明基本策略类,并将所有子类声明放在实现文件中。代码示例如下:

- 头文件

class MyUglyClass
{
private:
    class IStrategyBase;
    IStrategyBase* sPtr;
    // class ActualImplementation; // this is what I'd like to avoid
    // class YetAnotherImplementation; // as above
    // blah blah blah
};

- 实施档案

class MyUglyClass::IStrategyBase
{
    virtual ResultType DoSomething(SomeType someParameter) = 0;
    // could expose some MyUglyClass members, since 
    // derived classes wouldn't inherit friendship
};

class ActualImplementation: public MyUglyClass::IStrategyBase
{
    ResultType DoSomething(SomeType someParameter) override
    {
        // Do actual work
    }
}

class YetAnotherImplementation: public MyUglyClass::IStrategyBase
{
    ResultType DoSomething(SomeType someParameter) override
    {
        // Doing something really tricky & clever for corner cases
    }
}

当然编译器抱怨因为IStrategyBase无法访问;我可以通过fwd声明ActualImplementationYetAnotherImplementationIStrategyBase一起进入头文件来解决这个问题,但我宁愿避免这种情况,因为我需要更改标头需要新战略。

我也可以在公共范围内声明IStrategyBase,但我宁愿将其保密,以避免其他人搞乱它。

当然我假设非fwd声明的子类不会继承MyUglyClass的友谊,所以我必须公开IStrategyBase受保护成员的相关数据。

有没有办法实现这一点我可能会失踪?

修改

感谢所有评论过的人,我意识到即使在公共范围内声明,也没有人会混淆IStrategyBase类,因为类定义也会隐藏在实现文件中。我现在想知道的是,如果我可以使派生类访问MyUglyClass的内部,而不必将它们与IStrategyBase一起声明。我猜答案是“不”,因为友谊不是继承的,但也许还有一些我缺少的C ++特权。

2 个答案:

答案 0 :(得分:3)

一种可能性(这不是pimpl成语,只是一种可访问性黑客):

class MyUglyClass
{
private:
    struct Impl; // Is automatically "friend struct Impl;"
    class IStrategyBase;
    IStrategyBase* sPtr;
    // class ActualImplementation; // this is what I'd like to avoid
    // class YetAnotherImplementation; // as above
    // blah blah blah
};

class MyUglyClass::IStrategyBase
{
public:
    virtual int DoSomething(int someParameter) = 0;
    // could expose some MyUglyClass members, since 
    // derived classes wouldn't inherit friendship
};

struct MyUglyClass::Impl
{
    class ActualImplementation: public MyUglyClass::IStrategyBase
    {
        int DoSomething(int someParameter) override
        { (void) someParameter; return 1;}
    };

    class YetAnotherImplementation: public MyUglyClass::IStrategyBase
    {
        int DoSomething(int someParameter) override
        { (void) someParameter; return 2; }
    };
};

int main() {}

答案 1 :(得分:2)

如果你想隐藏任何细节,你可以使用pImpl idiom(指向实现的指针)又名不透明指针https://en.wikipedia.org/wiki/Opaque_pointer所以你可以改变你的代码

- 头文件

#include <memory>

class MyUglyClass
{
    MyUglyClass();
    ~MyUglyClass(); // destructor must be only declared to avoid problems
                    // with deleting just forwarded inner class
private:
    class Impl;
    std::unique_ptr<Impl> pImpl;
};

- 实施档案

class MyUglyClass::Impl
{
    class IStrategyBase;
    IStrategyBase* sPtr;
    class ActualImplementation; // now these classes safely hidden inside .cpp 
    class YetAnotherImplementation; // Nobody can reach them.

};

class MyUglyClass::Impl::IStrategyBase
{
    virtual ResultType DoSomething(SomeType someParameter) = 0;
    // could expose some MyUglyClass members, since 
    // derived classes wouldn't inherit friendship
};

class ActualImplementation: public MyUglyClass::Impl::IStrategyBase
{
    ResultType DoSomething(SomeType someParameter) override
    {
        // Do actual work
    }
};

class YetAnotherImplementation: public MyUglyClass::Impl::IStrategyBase
{
    ResultType DoSomething(SomeType someParameter) override
    {
        // Doing something really tricky & clever for corner cases
    }
};

MyUglyClass::MyUglyClass() : pImpl(new Impl()) {}

MyUglyClass::~MyUglyClass() {} // let the unique_ptr do its work