TITLE: Virtual base class construction order


PROBLEM: schmidt@net6.ics.uci.edu (Douglas C. Schmidt)

class A { public:
  A (int i): j (i) { printf ("in A, i = %d\n", i); }
};

class B : virtual public A { public:
  B (int i): A (i) { printf ("in B, i = %d\n", i); }
};

class C : virtual public A { public:
  C (int i): A (i) { printf ("in C, i = %d\n", i); }
};

class D : public B, public C { public:
  D (int i): B (i), C (i) { printf ("in D, i = %d\n", i); }
};


RESPONSE: Steve Clamage, TauMetric Corp, steve@taumet.com

The problem here is that the virtual base class A must be constructed
before entering D's constructor or initializing the B and C base
classes.  Since there is no explicit initializer for A in D's init
list, the default constructor must be used -- but there isn't one.
Hence the error message.

When you gave A::A(int) a default argument, that made it a default
constructor, and the code became legal, if not what you wanted.

You can also add an initializer for A to D's init list:
	D (int i): A(i), B(i), C(i) { printf ("in D, i = %d\n", i); }





