🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Casting within MI Graphs

#418
From: Scott Meyers
Date: Fri, 24 Aug 1990 19:28
74 lines
2845 bytes
Does E&S prohibit explicit casts from pointers to virtual base classes to
pointers to derived classes?  The only thing I can find is in the
commentary on page 227:

    Casting from a virtual base class to a derived class is disallowed to
    avoid requiring an implementation to maintain pointers to enclosing
    objects. 

However, this passage is commentary, not proposed law.  Is there such a
restruction proposed for standard C++?

A similar question arises from the commentary on page 222:

    Casting a value to a pointer to a base class and then to some other
    type may not yield the same result as casting directly to the second
    type. 

Again, is this merely commentary, or is it part of the proposed language
standard?  In general, I don't have a problem with it, but when casting up
and down an inheritance hierarchy, I would like casts to always "do the
right thing."  That is, given:

        A      
       / \     
      /   \     A is the top of the hierarchy and may be a virtual base
     B     C    class for B and C.
      \   /
       \ /
        D

    A *p = new D;
    void *pv;

I want the following to all yield equivalent results:

    pv = p;
    p = (D *) pv;

    pv = (B *) p;
    p = (D *) (A *) pv;

    pv = (A *) p;
    p = (D *) (B *) pv;

    etc.

Ditto for these:

    pv = p;
    p = (B *) pv;
    p = (B *) (D *) pv;

    etc.

These considerations are important in the common case where A, B, and C are
all abstract classes, so collections of A, B, and C pointers are really all
collections of pointers to D objects.  If casting order makes a difference,
then it's not enough to know that everything in a list of A pointers is
really a D pointer, because you have to know the order in which pointer
casts were applied if you want to reliably convert the underlying D
pointers to A, B, C, or D pointers.  Similarly, if casts from virtual bases
to derived objects are disallowed, then collections of pointers to A
objects are next to useless.

To summarize:

    - Casts from pointer-to-virtual-base to pointer-to-derived should be
      allowed.

    - The order of application of casts between object pointer types within
      an inheritance graph should not matter in terms of the final result.
      (Obviously, user-defined casting functions are excluded.)

Scott
Technical Details
Message-ID: <48168@brunix.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!uunet!brunix!sdm

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!uunet!brunix!sdm
From: sdm@cs.brown.edu (Scott Meyers)
Newsgroups: comp.std.c++
Subject: Casting within MI Graphs
Message-ID: <48168@brunix.UUCP>
Date: 24 Aug 90 19:28:07 GMT
Sender: news@brunix.UUCP
Reply-To: sdm@cs.brown.edu (Scott Meyers)
Organization: Brown University Department of Computer Science
Lines: 74