In C++, what is a virtual base class?

后端 未结 11 2142
一个人的身影
一个人的身影 2020-11-22 00:55

I want to know what a \"virtual base class\" is and what it means.

Let me show an example:

class Foo
{
public:
    void DoSomething() { /* .         


        
相关标签:
11条回答
  • 2020-11-22 01:21

    About the memory layout

    As a side note, the problem with the Dreaded Diamond is that the base class is present multiple times. So with regular inheritance, you believe you have:

      A
     / \
    B   C
     \ /
      D
    

    But in the memory layout, you have:

    A   A
    |   |
    B   C
     \ /
      D
    

    This explain why when call D::foo(), you have an ambiguity problem. But the real problem comes when you want to use a member variable of A. For example, let's say we have:

    class A
    {
        public :
           foo() ;
           int m_iValue ;
    } ;
    

    When you'll try to access m_iValue from D, the compiler will protest, because in the hierarchy, it'll see two m_iValue, not one. And if you modify one, say, B::m_iValue (that is the A::m_iValue parent of B), C::m_iValue won't be modified (that is the A::m_iValue parent of C).

    This is where virtual inheritance comes handy, as with it, you'll get back to a true diamond layout, with not only one foo() method only, but also one and only one m_iValue.

    What could go wrong?

    Imagine:

    • A has some basic feature.
    • B adds to it some kind of cool array of data (for example)
    • C adds to it some cool feature like an observer pattern (for example, on m_iValue).
    • D inherits from B and C, and thus from A.

    With normal inheritance, modifying m_iValue from D is ambiguous and this must be resolved. Even if it is, there are two m_iValues inside D, so you'd better remember that and update the two at the same time.

    With virtual inheritance, modifying m_iValue from D is ok... But... Let's say that you have D. Through its C interface, you attached an observer. And through its B interface, you update the cool array, which has the side effect of directly changing m_iValue...

    As the change of m_iValue is done directly (without using a virtual accessor method), the observer "listening" through C won't be called, because the code implementing the listening is in C, and B doesn't know about it...

    Conclusion

    If you're having a diamond in your hierarchy, it means that you have 95% probability to have done something wrong with said hierarchy.

    0 讨论(0)
  • 2020-11-22 01:26

    Virtual base classes, used in virtual inheritance, is a way of preventing multiple "instances" of a given class appearing in an inheritance hierarchy when using multiple inheritance.

    Consider the following scenario:

    class A { public: void Foo() {} };
    class B : public A {};
    class C : public A {};
    class D : public B, public C {};
    

    The above class hierarchy results in the "dreaded diamond" which looks like this:

      A
     / \
    B   C
     \ /
      D
    

    An instance of D will be made up of B, which includes A, and C which also includes A. So you have two "instances" (for want of a better expression) of A.

    When you have this scenario, you have the possibility of ambiguity. What happens when you do this:

    D d;
    d.Foo(); // is this B's Foo() or C's Foo() ??
    

    Virtual inheritance is there to solve this problem. When you specify virtual when inheriting your classes, you're telling the compiler that you only want a single instance.

    class A { public: void Foo() {} };
    class B : public virtual A {};
    class C : public virtual A {};
    class D : public B, public C {};
    

    This means that there is only one "instance" of A included in the hierarchy. Hence

    D d;
    d.Foo(); // no longer ambiguous
    

    This is a mini summary. For more information, have a read of this and this. A good example is also available here.

    0 讨论(0)
  • 2020-11-22 01:26

    Virtual classes are not the same as virtual inheritance. Virtual classes you cannot instantiate, virtual inheritance is something else entirely.

    Wikipedia describes it better than I can. http://en.wikipedia.org/wiki/Virtual_inheritance

    0 讨论(0)
  • 2020-11-22 01:35

    It means a call to a virtual function will be forwarded to the "right" class.

    C++ FAQ Lite FTW.

    In short, it is often used in multiple-inheritance scenarios, where a "diamond" hierarchy is formed. Virtual inheritance will then break the ambiguity created in the bottom class, when you call function in that class and the function needs to be resolved to either class D1 or D2 above that bottom class. See the FAQ item for a diagram and details.

    It is also used in sister delegation, a powerful feature (though not for the faint of heart). See this FAQ.

    Also see Item 40 in Effective C++ 3rd edition (43 in 2nd edition).

    0 讨论(0)
  • 2020-11-22 01:36

    I'd like to add to OJ's kind clarifications.

    Virtual inheritance doesn't come without a price. Like with all things virtual, you get a performance hit. There is a way around this performance hit that is possibly less elegant.

    Instead of breaking the diamond by deriving virtually, you can add another layer to the diamond, to get something like this:

       B
      / \
    D11 D12
     |   |
    D21 D22
     \   /
      DD
    

    None of the classes inherit virtually, all inherit publicly. Classes D21 and D22 will then hide virtual function f() which is ambiguous for DD, perhaps by declaring the function private. They'd each define a wrapper function, f1() and f2() respectively, each calling class-local (private) f(), thus resolving conflicts. Class DD calls f1() if it wants D11::f() and f2() if it wants D12::f(). If you define the wrappers inline you'll probably get about zero overhead.

    Of course, if you can change D11 and D12 then you can do the same trick inside these classes, but often that is not the case.

    0 讨论(0)
提交回复
热议问题