This is a virtual output property.
The idea behind virtual output is to solve the Scary Diamond Pattern:
struct Base {}; struct D1: Base {}; struct D2: Base {}; struct TopDiamond: D1, D2 {};
The problem here is that TopDiamond has 2 Base instances here.
To solve this problem, a very peculiar "MultiInheritance", C ++ uses the virtual and what is called the so-called "virtual inheritance".
If we change the way D1 and D2 , we define such that:
struct D1: virtual Base {}; struct D2: virtual Base {};
Then TopDiamond will be only one Base instance in TopDiamond : the task of its actual instance remains in the upper constructor (here TopDiamond ).
So the little trick you showed is simply explained here:
- because
Usable obtained practically from Usable_lock , it creates an instance of the Usable_lock part of the object before its derived class - Since the constructor is
Usable_lock private , only Usable (friend) can access the constructor
This is smart, I never thought about it. I wonder what is the virtual inheritance cost here (extra memory / overhead speed)?
source share