Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Late implementation of an interface

Tags:

c++

interface

I have a class Foo.

struct Foo
{
    void someFunc()
    {
    }
};

I have an interface IFoo.

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

If I didn't want to implement IFoo into Foo directly, is there a way to do it at a later point?

...

A FAILED attempt was to do this: Create a class which implements them both... THEORETICALLY satisfying IFoo by also inheriting from Foo.

struct Bar : Foo, IFoo
{

};

Which could be used like this:

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

But that didn't work. The compiler sees Bar as abstract.

Does anyone have any ideas please? There is no actual code problem to paste, I am just trying to see if something like this would be possible.

like image 566
Beakie Avatar asked Sep 18 '14 11:09

Beakie


1 Answers

struct Bar : IFoo, Foo
{
    virtual void someFunc()
    {
        Foo::someFunc();
    }
};

or:

struct Bar : IFoo
{
    Foo foo;    
    virtual void someFunc()
    {
        foo.someFunc();
    }
};
like image 57
Piotr Skotnicki Avatar answered Sep 27 '22 02:09

Piotr Skotnicki