How can I implement my explicit override outside the class declaration?

1k views Asked by At

I have two classes with the same pure virtual method:

class InterfaceA
{
    public: virtual void doSomething() = 0;
};
class InterfaceB
{
    public: virtual void doSomething() = 0;
};

And I have a class that derives from these interfaces. I want to override each virtual function. I can do it this way (this works):

class ConcreteClass : public InterfaceA, public InterfaceB
{
    public:

    void InterfaceA::doSomething() override
    {
        printf( "In ConcreteClass::InterfaceA::doSomething()\n" );
    }

    void InterfaceB::doSomething() override
    {
        printf( "In ConcreteClass::InterfaceB::doSomething()\n" );
    }
};

My question is however, how can I have my methods' definitions outside the class declaration? So I can have them in my .cpp file. I tried this first:

// .h
class ConcreteClass : public InterfaceA, public InterfaceB
{
    public:

    void InterfaceA::doSomething() override;
    void InterfaceB::doSomething() override;
};
// .cpp
void ConcreteClass::InterfaceA::doSomething()
{
    printf( "In ConcreteClass::InterfaceA::doSomething()\n" );
}
void ConcreteClass::InterfaceB::doSomething()
{
    printf( "In ConcreteClass::InterfaceB::doSomething()\n" );
}

This doesn't compile in Visual C++ 2005 (VS 2005):

error C2509: 'doSomething' : member function not declared in 'ConcreteClass'

Does it require a specific sintax in order to be compiled?

Microsoft's MSDN documentation has a working example. But they use their __interface extension. I want to achieve the same but code that complies with standard c++03, if it is even possible.

Thanks!

1

There are 1 answers

0
AudioBubble On

Even though it's not exactly what you are looking for, the obvious workaround is to dispatch to helper functions:

class ConcreteClass : public InterfaceA, public InterfaceB
{
public:
    void InterfaceA::doSomething() override {
        InterfaceA_doSomething();
    }

    void InterfaceB::doSomething() override {
        InterfaceB_doSomething();
    }

 private:
    void InterfaceA_doSomething();
    void InterfaceB_doSomething();
};