With virtual inheritance the most derived class must call the constructor of the virtual base class (in contrast to non-virtual inheritance where each class constructs its direct bases). Because MakeFinal
s constructor is private
and only Final
is a friend, no other classes can access MakeFinal
s constructor. Consequently you cannot derive from Final
. More accurately you can derive:
struct OK : Final {};
but you cannot create instances, because constructing a OK
would require to call the private MakeFinal
constructor:
OK not_ok; // error: use of deleted function 'OK::OK()'
Without virtual inheritance this wouldn't work as intended, because then OK
s constructor would call Final
s constructor which in turn calls MakeFinal
s constructor. All would be fine. Only with virtual inheritance the most derived class must call the virtual base class constructor.
Note that since C++11, this "simulation" is not required. There is final
to explicitly make a class final.
[...] without any compilation error to demonstrate that instances of the Final class can be created
Not sure if this is a misunderstanding. Of course you can create instances of a final class. When a class is final you just cannot inherit from it any further. (As explained above in your "simulation" one can inherit from Final
, its just pretty useless because no instances can be constructed of a class inheriting from Final
).