Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Maybe I’m misunderstanding, but by virtual base classes you mean base classes with strictly pure abstract methods, that can be used with virtual inheritance?

No. Virtual base classes come up in multiple inheritance.

Consider you have a class Circle. It derives from both Shape and Savable. But Shape and Savable both derive from a parent base class Base. Let's say that Base has some member function, print. Also assume that neither Circle, Shape or Savable have an implementation of print. So when you have a Circle object, and you call print, does it go to Shape::print or Savable::print? If Base is a normal (non-virtual) base class, it's ambiguous. The compiler can't resolve it, as the Circle object literally has two Base instances inside of it: one from Shape and one from Drawable. The inheritance hierarchy has two roots, both of which are an instance of Base.

But if Base is a virtual base class, then Circle will only have one instance of Base. The inheritance hierarchy has only one root, which is Base.

This is generally called "the diamond problem": https://stackoverflow.com/questions/2659116/how-does-virtual...



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: