Search code examples
c++inheritanceambiguous

C++ Ambiguity issue


Let the following classes :

class BaseClass
{
    class OnSomeEventListener
    {
    public:
        enum SomeEnum { BLABLA }
        virtual void OnSomeEvent( SomeEnum eventData ) = 0;
    }
};

class ChildClass :
    public BaseClass,
    public BaseClass::OnSomeEventListener
{
    virtual void OnSomeEvent( BaseClass::OnSomeEventListener::SomeEnum eventData );
}

My question is : why do I need to specify BaseClass:: in front of OnSomeEventListener::SomeEnum eventData in the method virtual void OnSomeEvent( BaseClass::OnSomeEventListener::SomeEnum eventData ); ?

If I don't do it, it says that OnSomeEventListener is ambiguous between BaseClass::OnSomeEventListener and BaseClass::OnSomeEventListener::OnSomeEventListener

Why would it think i'm referencing the constructor instead of the OnSomeEventListener type ? Why would i need to prefix the argument type with BaseClass:: since I'm already inside BaseClass ?

Thank you.


Solution

  • Why would i need to prefix the argument type with BaseClass:: since I'm already inside BaseClass ?

    You are inside BaseClass, but you are also inside OnSomeEventListener because you inherit from both.

    When the compiler parses a name, it doesn't think "I need a type here, can this be an enum?", instead it thinks "I have a name here, what can it be?". And in your case it can be two different things, depending on which base class is searched for the name.