Thread View: comp.std.c++
2 messages
2 total messages
Started by weber
Wed, 21 Feb 1996 17:51
virtual base class initialization
Author: weber
Date: Wed, 21 Feb 1996 17:51
Date: Wed, 21 Feb 1996 17:51
77 lines
3075 bytes
3075 bytes
//
// 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
]
Re: virtual base class initialization
Author: Steve Clamage
Date: Wed, 21 Feb 1996 21:03
Date: Wed, 21 Feb 1996 21:03
34 lines
1820 bytes
1820 bytes
In article IAA16749@ncar.ucar.EDU, weber@ezibk8.vmsmail.ethz.ch writes:
>
>// I am not sure how to interprate the ARM (or the Draft C++ Standard)
>// with regard to the initialization of a virtual base class.
>
... V is a virtual base class of A
>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.
Yes it should. The most-derived class initializes all the virtual base
classes in its hierarchy, whether the virtual base classes are direct
base classes or not. None of the intermediate base classes initialize
any of the virtual base classes.
It would be possible to define other (probably more complicated) rules
to specify a different order of initialization, but that is the C++ rule.
---
Steve Clamage, stephen.clamage@eng.sun.com
[ 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
]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads