virtual base class

D

dumboo

hi there
i m having a base class

class base
{
...
};

and two derived class, which have inherited 'base' but the inheritance is
not virtual

class derived1 : public base
{
....
};

class derived2 : public base
{
....
};

now i m having another derived class, which requires functionality of both
derived1 and derived2 but since base class inheritance was not virtual, it
is giving me ambiguity problem

the problem here is that, i m having those classes base, derived1 and
derived2 as a part of a huge library, i m having access to just the header
files,

one solution here would be to modify the header file manually and add the
keyword 'virtual' and make inheriance of base class virtual for all the
derived class, derived1 and derived2

but that would require me to make those changes to those classes in each and
every header file, on the system where i want to compile my program, and
hence i would be loosnig protability

any suggestions ??
 
N

Niels Dybdahl

now i m having another derived class, which requires functionality of both
derived1 and derived2 but since base class inheritance was not virtual, it
is giving me ambiguity problem

How about pack either derived1 or derived2 into an additional class as
private and then recreate the needed functions in the new class:

class base {
public:
void f() {}
};

class derived1: public base {
public:
void f1() {}
};

class derived2: public base {
public:
void f2() {}
};

class derived1b: private derived1 {
public:
void fb() { f(); }
void f1b() { f1(); }
};

class derived: public derived1b, public derived2 {
};

Niels Dybdahl
 
K

Kench

dumboo said:
hi there
i m having a base class

class base
{
..
};

and two derived class, which have inherited 'base' but the inheritance is
not virtual

class derived1 : public base
{
...
};

class derived2 : public base
{
...
};

now i m having another derived class, which requires functionality of both
derived1 and derived2 but since base class inheritance was not virtual, it
is giving me ambiguity problem

the problem here is that, i m having those classes base, derived1 and
derived2 as a part of a huge library, i m having access to just the header
files,

one solution here would be to modify the header file manually and add the
keyword 'virtual' and make inheriance of base class virtual for all the
derived class, derived1 and derived2

but that would require me to make those changes to those classes in each and
every header file, on the system where i want to compile my program, and
hence i would be loosnig protability

any suggestions ??

Depending on what is feasible for your case you could
1. class derived3:public derived1, public derived2{ //here redifine
ambigugous funtions and in that call either derived1.f()
or derived2.f() or both. If you just need to call either derived1.f() you
can use using dervied::f;
2. class derived3:public derived1, private derived2 {}//Use the dervied1 as
interface and dervied2 as implementation/helper.
Hope this helps for your situation
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top