473,471 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What to do when base class is not specified as virtual?

Hi all,

Consider the situation. I am designing class library and I have a class
A. I derived class B and class C from A but derivation is not virtual.
for exa.

class A {};
class B : public A {};
class C : public A {};

So when I am designing this library, after research I come to
conclusion that nobody will derive from class B and class C together.
So I decided not derive it virtually. But in infinite future, if
somebody wants to derive from B and C, it will create two copy of A in
the derived class, let's call it D.

Now that person doesn't have access to modify my class library and if
he wants to reuse my code and derive from B and C together, will have
problem of having duplicate copy of A in the object of class D. Assume
that he has strong memory constraints and no way can afford to have
duplicate copy of A inside D.

So even though code is resuable, he can't use it. What can be the
solution of this? Does it mean I should always use virtual inheritance
during creation of B and C? Is there any drawback if I use virtual
inheritance all the time?

Thanks.

Jan 14 '06 #1
2 1645
ma*************@yahoo.com wrote:
Consider the situation. I am designing class library and I have a
class A. I derived class B and class C from A but derivation is not
virtual. for exa.

class A {};
class B : public A {};
class C : public A {};

So when I am designing this library, after research I come to
conclusion that nobody will derive from class B and class C together.
So I decided not derive it virtually. But in infinite future, if
somebody wants to derive from B and C, it will create two copy of A in
the derived class, let's call it D.

Now that person doesn't have access to modify my class library and if
he wants to reuse my code and derive from B and C together, will have
problem of having duplicate copy of A in the object of class D.
Assume that he has strong memory constraints and no way can afford to
have duplicate copy of A inside D.

So even though code is resuable, he can't use it. What can be the
solution of this? Does it mean I should always use virtual inheritance
during creation of B and C? Is there any drawback if I use virtual
inheritance all the time?


There is no solution to this, if the library is delivered in binary.
Instead, to let them fix it when they need it, ship the library in the
source code.

V
Jan 14 '06 #2
In article <11**********************@g14g2000cwa.googlegroups .com>,
ma*************@yahoo.com wrote:
Hi all,

Consider the situation. I am designing class library and I have a class
A. I derived class B and class C from A but derivation is not virtual.
for exa.

class A {};
class B : public A {};
class C : public A {};

So when I am designing this library, after research I come to
conclusion that nobody will derive from class B and class C together.
So I decided not derive it virtually. But in infinite future, if
somebody wants to derive from B and C, it will create two copy of A in
the derived class, let's call it D.

Now that person doesn't have access to modify my class library and if
he wants to reuse my code and derive from B and C together, will have
problem of having duplicate copy of A in the object of class D. Assume
that he has strong memory constraints and no way can afford to have
duplicate copy of A inside D.

So even though code is resuable, he can't use it. What can be the
solution of this? Does it mean I should always use virtual inheritance
during creation of B and C? Is there any drawback if I use virtual
inheritance all the time?


No you should not. If you do that then the users who want to derive from
both B & C *and* want two copies of A will have problems.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Jan 15 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Dario | last post by:
The following simple program behaves differently in Windows and Linux . #include <stdexcept> #include <iostream> #include <string> using namespace std; class LogicError : public logic_error {...
10
by: Peter Oliphant | last post by:
Is there a way of defining a method in a base class such that derived classes will call their own version, EVEN if the derived instance is referred to by a pointer to the base class? Note that the...
8
by: weird0 | last post by:
Can anyone explain briefly what is the difference between inheritance and polymorphism? i read and seem to forget it again and again... Can anyone along with good examples of c# explain the...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
6
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
1
by: Rune Allnor | last post by:
Hi all. I am sure this is an oldie, but I can't find a useful suggestion on how to solve it. I have a class hierarchy of classes derived from a base class. I would like to set up a vector of...
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
6
by: .rhavin grobert | last post by:
guess you have .... class Grandbase { public: virtual int foo(); }; class Base: public Grandbase { /* no foo() specified here */ };
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.