Multiple inheritance, dominance and pure base class

M

Matthias

Hello,

I have a class tree that looks like

class BaseInterface
{
public:
virtual void f1() = 0;
};

class DerivedInterface : public BaseInterface
{
public:
virtual void f2() = 0;
};

class CommonImplementation
{
public:
void f1() {}
};

class X : public DerivedInterface, public CommonImplementation
{
public:
void f2() {}
};

X* x = new X(); // <--- compiler error: can't instantiate
abstract clas, because f1 is still a pure virtual method

Why can't I instantiate X? And which workarounds are there? Naively I
would expect that the pure BaseInterface::f1() is overriden by
CommonImplementation::f1() and DerivedInterface::f2() is overriden by
X::f2().

Thanks for your help!

-Matthias
 
M

Matthias

If I change it to the following tree it works... Why does the first
method fail?

class BaseInterface
{
public:
virtual void f1() = 0;
};


class DerivedInterface
{
public:
virtual void f2() = 0;
};


class CommonImplementation : public BaseInterface
{
public:
void f1() {}
};


class X : public DerivedInterface, public CommonImplementation
{
public:
void f2() {}
};
 
V

Victor Bazarov

Matthias said:
If I change it to the following tree it works... Why does the first
method fail?
[..]

Because you can only override a virtual function (introduce the final
overrider) in a _descendant_ class, not sideways.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top