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

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

提交回复
热议问题