🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: casting and virtual inheritence

#3310
From: David Sachs
Date: Thu, 08 Feb 1996 22:56
51 lines
2401 bytes
cardboard.mti.sgi.com!box.mti.sgi.com!sgi.sgi.com!ncar.UCAR.EDU!uunet!chatz!chatz (David Chatterton) writes:

>Hi,

>There is very little on virtual inheritence in the draft. I cannot find nay
>statement saying that you cannot do this:

>class A {};
>class B : public virtual A {};

>int main()
>{
>    A* a1 = new A;
>    A* a2 = new B;
>    B* b1 = (B *)A;	// Illegal due to virtual inheritence
>    return 0;
>}

>Yet g++ and cfront won't let you. However, g++ (2.7.0) will let you do this:

>    B* b1 = dynamic_cast<B*>(A);

>Why can't you force the cast to a B (I guess it has to do with the virtual
>tables)? And is g++ correct in allowing dynamic_cast to do it?

First of all, I think (B*A) or dynamic_cast<B*>(A) is not a valid
expression, because A is a type name rather than a variable name. I
assume you meant dynamic_cast<B*>(a1)... In this case the semantics
are:


class A {};
class B : public virtual A {};

int main()
{
    A* a1 = new A;
    A* a2 = new B;
    B* b1 = dynamic_cast<B*>(a1);  // b1 == NULL, because there is no B
    B* b2 = dynamic_cast<B*>(a2);  // points to a2 as a B
    return 0;
}
-- 
* IRS, IRS, lord of the federal tax, checking all income and every deduction, *
* mailing out form 10 40, penalizing noncompliers, regulating, and auditing.  *
David Sachs - Fermilab, HPPC MS369 - P. O. Box 500 - Batavia, IL 60510
Voice: 1 708 840 3942      Deparment Fax: 1 708 840 3785
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
Technical Details
Message-ID: <4fdulf$34p@fsgm01.fnal.gov>
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern
References: <4fbt35$9ej@fido.asd.sgi.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern
From: b91926@fsgm01.fnal.gov (David Sachs)
Newsgroups: comp.std.c++
Subject: Re: casting and virtual inheritence
Date: 08 Feb 1996 14:56:31 PST
Organization: FERMILAB, Batavia, IL
Approved: austern@isolde.mti.sgi.com
Message-ID: <4fdulf$34p@fsgm01.fnal.gov>
References: <4fbt35$9ej@fido.asd.sgi.com>
Reply-To: sachs@fnal.fnal.gov
NNTP-Posting-Host: isolde.mti.sgi.com
X-Original-Date: 8 Feb 1996 16:49:19 -0600
X-Newsreader: NN version 6.5.0 #9 (NOV)
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBVAwUBMRp/rEy4NqrwXLNJAQHgcgH/Ts9VVT+AvAx7uxXQ6prWolPBuUKJiDZw
 7lls2BsorWSYqivQ1hqgVBpfZVpFw6NjOPU+vsPoMC5yrIHFRPIvKA==
 =+bcJ
Originator: austern@isolde.mti.sgi.com