🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: casting and virtual inheritence

#3307
From: Steve Clamage
Date: Fri, 09 Feb 1996 15:33
47 lines
2344 bytes
In article 9ej@fido.asd.sgi.com, cardboard.mti.sgi.com!box.mti.sgi.com!sgi.sgi.
com!ncar.UCAR.EDU!uunet!chatz!chatz (David Chatterton) writes:
>
>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;
>}

In the current draft standard, section 5.4 "Explicit type conversion (cast
notation)" lists exactly those conversions which may be performed, and
down-casting from a virtual base class is not among them.

In the ARM, section 5.4 "Explicit Type Conversion" says about the same
thing, the difference being that the four new cast keywords were not
in the language at that time.

In other words, the cast noted above has never been allowed in C++, and
still is not.

>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?

Dynamic_cast was created specifically to allow safe down-casting, and in
particular allows you to down-cast pointers or references from a virtual
base class to a derived class. The only requirement is that the base class
have one or more virtual functions. See section 5.2.6 "Dynamic cast" in the
current draft.

---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  Moderation policy:
  http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
Technical Details
Message-ID: <4fdo8b$6ce@engnews1.Eng.Sun.COM>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
References: <4fbt35$9ej@fido.asd.sgi.com>

Show Full Headers
Complete Article Headers
From: clamage@Eng.Sun.COM (Steve Clamage)
Message-ID: <4fdo8b$6ce@engnews1.Eng.Sun.COM>
X-Original-Date: 8 Feb 1996 20:59:55 GMT
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
Date: 09 Feb 96 15:33:46 GMT
Approved: fjh@cs.mu.oz.au
Newsgroups: comp.std.c++
Subject: Re: casting and virtual inheritence
Organization: Sun Microsystems Inc.
References: <4fbt35$9ej@fido.asd.sgi.com>
Reply-To: clamage@Eng.Sun.COM
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMRtpouEDnX0m9pzZAQFpWgF/R5F9HfFtnmznm3IMsZF19K4wXyYlQpXh
 OmFTaTU2isHnEM/uIVIZ9+sUwGnPOJOM
 =yIaJ