不可修复的循环依赖

Nic*_*ley 0 c++ inheritance

此代码error C2504: 'IKeyEvent': base class undefined在第3行给出.

class IKeyEvent;

class EventDispatcher : private IKeyEvent {
public:
    enum EEActions {
        A_FEW_ACTIONS
    };
private:
    void OnKey(EventDispatcher::EEActions action, char multiplier);
}

class IKeyEvent {
public:
    virtual void OnKey(EventDispatcher::EEActions action, char multiplier) = 0;
};
Run Code Online (Sandbox Code Playgroud)

在定义之前,您不能继承类,这是可以理解的.但是IKeyEvent直到定义之后 我才能EventDispatcher定义.

我知道我可以将其enum移出Event Dispatcher定义以使其成为全局,但这需要重构大部分程序.有没有办法EventDispatcher从依赖的类继承EventDispatcher

Mar*_*wis 5

我的建议:将EEActions移到基类中 - 毕竟它界面的一部分:

class IKeyEvent {
public:
    enum EEActions {
        A_FEW_ACTIONS
    };
    virtual void OnKey(EEActions action, char multiplier) = 0;
};

class EventDispatcher : public IKeyEvent {
private:
    void OnKey(EventDispatcher::EEActions action, char multiplier);
};
Run Code Online (Sandbox Code Playgroud)

如果您还从IKeyEvent公开继承,则可以继续将枚举引用为EventDispatcher::EEActions(尽管在基类型中定义了枚举).