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

后端 未结 11 2151
一个人的身影
一个人的身影 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: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.

提交回复
热议问题