I want to know what a "virtual base class" is and what it means.
Let me show an example:
class Foo
{
public:
void DoSomething() { /* ... */ }
};
class Bar : public virtual Foo
{
public:
void DoSpecific() { /* ... */ }
};
I want to know what a "virtual base class" is and what it means.
Let me show an example:
class Foo
{
public:
void DoSomething() { /* ... */ }
};
class Bar : public virtual Foo
{
public:
void DoSpecific() { /* ... */ }
};
In addition to what has already been said about multiple and virtual inheritance(s), there is a very interesting article on Dr Dobb's Journal: Multiple Inheritance Considered Useful
Diamond inheritance runnable usage example
This example shows how to use a virtual base class in the typical scenario: to solve diamond inheritance.
Explaining multiple-inheritance with virtual bases requires a knowledge of the C++ object model. And explaining the topic clearly is best done in an article and not in a comment box.
The best, readable explanation I found that solved all my doubts on this subject was this article: http://www.phpcompiler.org/articles/virtualinheritance.html
You really won't need to read anything else on the topic (unless you are a compiler writer) after reading that...
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
You're being a little confusing. I dont' know if you're mixing up some concepts.
You don't have a virtual base class in your OP. You just have a base class.
You did virtual inheritance. This is usually used in multiple inheritance so that multiple derived classes use the members of the base class without reproducing them.
A base class with a pure virtual function is not be instantiated. this requires the syntax that Paul gets at. It is typically used so that derived classes must define those functions.
I don't want to explain any more about this because I don't totally get what you're asking.
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).