Topic: virtual base class initialization
Author: weber@ezibk8.vmsmail.ethz.ch
Date: 1996/02/21 Raw View
//
// I am not sure how to interprate the ARM (or the Draft C++ Standard)
// with regard to the initialization of a virtual base class. I have tried
// some examples on a DEC C++ compiler, but I am not sure whether the results
// are o.k. Could anybody give me some details?
//
// Benedikt Weber, Swiss Federal Institute of Technology, Zurich
// weber@ibk.baum.ethz.ch
//*************************************************************************
// Here some problems initializing a virtual base class V
// The results indicated were obtained with the DEC C++ compiler
#include <iostream.hxx>
class V{
public:
V(int i) {cout << "init V(int)" << endl;}
V() {cout << "init V()" << endl;}
};
class A: virtual public V{
public:
A() {}
A(int i): V(i) {}
};
class B: virtual public V{
};
class C1: public A, public B, virtual public V{
public:
C1(int i): A(i) {}
};
// C1 is similar to the ARM example at the end of Section 12.6.2
// and works correctly. V is initialized directly by the most derived class
// C1 as V(). My example prints correctly "init V()"
class C2: public A, public B{
public:
C2(int i): A(i){}
};
// C2 is not directly derived from V. In this case I don't think the base
// class should still be initialized by the most derived class. Rather
// C2 should initialize A(i) which in turn sould initialize V(i).
// This would be in accordance with the example in the ARM in Section 12.6.2
// where the order of initializatoin is explained. However, the ARM is not
// quite clear in this point and it could be a question of interpretation.
//
// My example prints "init V()", i.e. it initializes V directly from
// C2, which seems not correct to me.
//
class C3: public A, public B{
public:
C3(int i): V(i){}
};
// C3 is not directly derived from V. Nevertheless it does initialze V(i)
// and the example prints "init V(int)". I wonder whether this is correct
// because I think a class can only initialize it's direct base classes.
int main(){
C1(1);
C2(2);
C3(3);
}
---
[ To submit articles: Try just posting with your newsreader. If that fails,
use mailto:std-c++@ncar.ucar.edu
FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]