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

M

mac_ferrari1975

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.
 
V

Victor Bazarov

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
 
D

Daniel T.

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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top