🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: dynamic_cast<T> implementation...

#2271
From: Alex Martelli
Date: Sat, 21 Oct 1995 05:45
39 lines
2735 bytes
"Michael McCarty" <mikem@eai.com> writes:
	...
>How exactly is dynamic_cast<T> supposed to be implemented in the compiler?

The standard, of course, does not specify that.

>It seems to me that the only way to implement this is to attach a type
>'pointer' to every object instantiated, similar to virtual function pointer

In "dynamic_cast<T> v", except for quite a few cases which can be solved
at compile time (e.g., where type T is a pointer to a base class of what
v points to), v "shall be a pointer to or an lvalue of a polymorphic type"
(cfr expr.dynamic.cast sub 6).  Therefore a "type 'pointer'" or equivalent
functionality will only be needed for types that have at least one vptr
anyway, and indeed one could imagine minimizing the overhead by enriching
the vtable structure -- e.g. just adding to each vtable of a derived
class a pointer to the vtable of the parent class (assuming a multiply
derived class has separate vtables for its various derivations); in
which case, on "dynamic_cast<D*> bp" (with bp declared as a B* and
D deriving from B), the compiler might run-time check by following
the chain of such pointers upwards from *bp's (along the appropriate
inheritance path towards B) and see if it encounters the vtable of
class D (relevant to its descending from B).  I'm sure far more clever
implementations exist (ones that pay a constant time for the check
rather than one proportional to the number of levels of derivation),
but this at least shows that the space overheads need not be much --
just one extra pointer per vtable of each polymorphic class...


Alex
--
DISCLAIMER: these are TOTALLY personal opinions and viewpoints, NOT connected
in any way with my employer, nor any other organization or individual!
Email: martelli@cadlab.it                            Phone: +39 (51) 597313
CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia   Fax: +39 (51) 597120
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <martelli.814182405@cadlab>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
References: <9510191610.ZM21213@yosemite.eai.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 21 Oct 95 05:45:42 GMT
Approved: fjh@cs.mu.oz.au
Newsgroups: comp.std.c++
From: martelli@cadlab.cadlab.it (Alex Martelli)
Subject: Re: dynamic_cast<T> implementation...
Message-ID: <martelli.814182405@cadlab>
X-Newsreader: NN version 6.5.0 #11
Organization: Cad.Lab Spa, Bologna, Italia
References: <9510191610.ZM21213@yosemite.eai.com>
X-Original-Date: Fri, 20 Oct 1995 09:46:45 GMT
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMIiJL+EDnX0m9pzZAQELZAF+NyZvFM2GJTXUe9Q94MR5JRNsoPvvD2yD
 NpCrupGAUa1S/0tIv7iB8eNATAabwi6O
 =ICgQ