It's a property of virtual derivation.
The idea of virtual derivation is to solve the "Dreaded Diamond Pattern":
struct Base {};
struct D1: Base {};
struct D2: Base {};
struct TopDiamond: D1, D2 {};
The problem here is that TopDiamond has 2 instance of Base here.
To solve this problem, very peculiar "MultiInheritance", C++ uses the virtual keyword and what is thus called "virtual inheritance".
If we change the way D1 and D2 are defined such that:
struct D1: virtual Base {};
struct D2: virtual Base {};
Then there will only be one instance of Base within TopDiamond: the job of actually instantiating it is left to the top-constructor (here TopDiamond).
Thus, the little trick you have shown is simply explained here:
- because
Usable derives virtually from Usable_lock, it's up to its derived class to instantiate the Usable_lock part of the object
- because
Usable_lock constructor is private, only itself and Usable (friend) can access the constructor
It's clever, I had never thought of that. I wonder what the cost of virtual inheritance is here (extra memory / speed overhead) ?