Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use inheritance to implement an interface in C++? [duplicate]

Possible Duplicate:
Implementing abstract class members in a parent class
Why does C++ not let baseclasses implement a derived class' inherited interface?

Considering these objects:

struct A
{
    virtual void foo() = 0;
};

struct B
{
    void foo() { /* neat implementation */ }
};

I wonder why --compiler-wise-- the following object is considered abstract:

struct C : B, A
{
    using B::foo; // I tried my best to make the compiler happy
};

The compiler won't let me do this:

A* a = new C;

Visual Studio 2010 says:

'C' : cannot instantiate abstract class due to following members: 'void A::foo(void)' : is abstract : see declaration of 'A::foo'

g++ 4.6.3 says:

cannot allocate an object of abstract type ‘C’ because the following virtual functions are pure within ‘C’: virtual void A::foo()

I guess this is allowed in C#, but I don't know the involved mecanism(s) -- I am just being curious.

like image 493
mister why Avatar asked May 10 '12 12:05

mister why


1 Answers

Unfortunately, this does not quite do what you want.

A using directive is just about bringing some names into the scope it is used. virtual methods need be overriden, and just bringing names is not considered overriding.

The truth of the matter is that C++ does not support delegation directly. There is just this slight twist of bringing names into scope explicitly.

like image 66
Matthieu M. Avatar answered Sep 22 '22 07:09

Matthieu M.