TITLE: Why is virtual base class initialization the way it is? PROBLEM: hanlon@birch307.cray.com (Jim Hanlon) I ran headlong into the behavior dictated by the paragraph on page 293 of ARM section 12.6.2 and am looking for clues into *why* virtual base class initialization is specified as it is? ... My question is "Why?" This specification seems to blow all hope of encapsulation out of the water, since E must know all the details of it's derivation graph. I can't see the need or benefit in this form of initialization. [ The complaint is that virtual base classes must have their constructors called explicitly by all direct AND INDIRECT descendants. -adc ] RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 10 Apr 95 The initialization rules pretty much fall out of other requirements. 1. A base-class subobject must be initialized before its derived-class subobjects. 2. An object contains only one copy of a virtual base class no matter how many times it appears in the inheritance graph. 3. An intermediate base class doesn't know about its descendants or siblings. 4. Two intermediate base class constructors might have different ideas about the initialization of what they don't know is a shared base class. Example: class VB { ... VB(int); ... }; class A : virtual public VB { A() : VB(10) { ... } // A knows nothing about B }; class B : virtual public VB { B() : VB(20) { ... } // B knows nothing about A }; class C : public A, public B { C() { ... } // Is VB initialized with 10 or with 20? }; The contructors of A, B, and C are all allowed to assume that VB is initialized, so VB must be initialized first. A and B don't know about each other, and conflict on initializatin of VB. The necessary information comes together only at a C constructor. Resolving all these issues either leads to a language different from C++, or to the C++ rules we have, or to a mess, as far as I can see. (One could argue that we have a mess anyway, of course :-) In particular, one of the desired features of multiple inheritance is the ability to combine different classes that have no knowledge of or dependence on one another. There is a clean solution, as John Skaller points out. A virtual base class should probably contain no data, and should have no constructors that require parameters (except for the copy ctor). Such a class is used only to define an interface. Initialization of such virtual base classes is not an issue.