🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3380

virtual base class initialization

#3380
From: weber
Date: Wed, 21 Feb 1996 17:51
77 lines
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 
]
Technical Details
Message-ID: <199602211555.IAA16749@ncar.ucar.EDU>
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern
From: weber@ezibk8.vmsmail.ethz.ch
Newsgroups: comp.std.c++
Subject: virtual base class initialization
Date: 21 Feb 1996 09:51:38 PST
Organization: -
Approved: austern@isolde.mti.sgi.com
Message-ID: <199602211555.IAA16749@ncar.ucar.EDU>
NNTP-Posting-Host: isolde.mti.sgi.com
X-Original-Date: Wed, 21 Feb 1996 16:54:27 +0100
X-Vms-To: ETHZ::"std-c++@ncar.ucar.edu"
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBVAwUBMStcL0y4NqrwXLNJAQFDNAH+OIJK56J+kDh4RyDU/daSDyZxTYnO/vLU
 /Fah8plcsqSi58xOSx7/iZ2hZF3YVy96xGJp7RYXfTpI60SFYAsprw==
 =nAFb
Originator: austern@isolde.mti.sgi.com