Late interface implementation

I have a Foo class.

struct Foo
{
    void someFunc()
    {
    }
};

I have an IFoo interface.

struct IFoo
{
    virtual void someFunc() = 0;
};

If I did not want to implement IFoo in Foo directly, is there a way to do this at a later point?

...

The failed attempt was as follows: Create a class that implements them as ... Theoretically satisfies IFoo, also inheriting from Foo.

struct Bar : Foo, IFoo
{

};

What can be used as follows:

Bar x = Bar();
IFoo* y = &x;

But that did not work. The compiler considers Bar as abstract.

Does anyone have any ideas? There is no problem with the embed code, I'm just trying to see if something like this is possible.

+4
source share
1 answer
struct Bar : IFoo, Foo
{
    virtual void someFunc()
    {
        Foo::someFunc();
    }
};

or

struct Bar : IFoo
{
    Foo foo;    
    virtual void someFunc()
    {
        foo.someFunc();
    }
};
+7
source

All Articles