A little doubt about Inheritance
Hi,
I'm sorry for the "stupid" question...
In a my project I declare some interfaces to expose a c++ ABI from my library.
But maybe I forgot some rules.
Why this code does not work?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include<iostream>
using namespace std;
class IBase
{
public:
virtual void foo() = 0;
};
class IBaseA : public IBase
{
public:
virtual void foo_bar_A() = 0;
};
class BaseImpl : IBase
{
public:
void foo(){cout << "BaseImpl::foo" << endl;};
};
class BaseAImpl : public IBaseA, public BaseImpl
{
public:
void foo_bar_A(){cout << "BaseAImpl::foo_bar_A" << endl;};
};
int _tmain(int argc, _TCHAR* argv[])
{
IBaseA* A = new BaseAImpl;
A->foo_bar_A();
IBase* B = (IBase*)A;
B->foo();
delete A;
return 0;
}
|
The compiler says that A is abstract.
Of course, not deriving IBaseA from IBase, make it works:
|
class IBaseA //: public IBase
|
But in this way in the output I get:
BaseAImpl::foo_bar_A
BaseAImpl::foo_bar_A
Daniele.
Last edited on
BaseAImpl inherits IBase twice:
IBaseA - IBase
/
BaseAImpl
\
BaseImpl - IBase
|
You can solve it adding virtual when inheriting IBase:
class IBaseA : virtual public IBase
class BaseImpl : virtual IBase
Last edited on
Thanks a lot Bazzy!
Daniele.
Topic archived. No new replies allowed.