473,385 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

virtual base class

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
2 1541
> 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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Michael Winter | last post by:
Until about 5 minutes ago, I was happy with my knowledge of virtual functions - then I read "Mixing interface and functional inheritance" posted by Kevin L. earlier today. All of a sudden, I found...
24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
2
by: junw2000 | last post by:
In the following code: #include <iostream> using namespace std; class V { public: int i; virtual void f() { cout << "V::f()" << endl;}
2
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
5
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
7
by: dragoncoder | last post by:
Hello experts, I was just playing around wrote this code. sundev1:/home/ptiwary/rnd $ cat a1.cpp #include <iostream> using namespace std; class Base
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.