By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

virtual base class

P: n/a
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 ??
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> 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
Jul 22 '05 #2

P: n/a

"dumboo" <vt***@yahoo.com> wrote in message
news:2h************@uni-berlin.de...
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
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.