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

Discussion in 'C++' started by mac_ferrari1975@yahoo.com, Jan 14, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jan 14, 2006
    #2
    1. Advertising

  3. Daniel T. Guest

    In article <>,
    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.
    Daniel T., Jan 15, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page