home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 33,064 of 33,346   
   Wil Evers to Francis Glassborow   
   Re: [Solved] sequence of inheritance fro   
   20 May 13 12:45:13   
   
   From: bouncer@dev.null   
      
   Francis Glassborow wrote:   
   > On 20/05/2013 00:39, Wil Evers wrote:   
   >> Francis Glassborow wrote:   
   [...]   
   >>> IIUC, the implementation is at liberty to lay out a derived class'   
   >>> base classes anyway it likes. There are, IIRC, some limitations on   
   >>> the relationship between a pointer to the derived class and a   
   >>> pointer to the first base class in the declaration of the derived   
   >>> class.   
   >>   
   >> Are you sure?  As far as I know, if we have...   
   >>   
   >> class Base { /* ... */ };   
   >> class Derived : public Base { /* ... */ };   
   >>   
   >> ...then for a Derived object, the compiler is free to either use a   
   >> layout in which the Base part comes first, or one in which the   
   >> Derived part comes first (which would not be very practical, but   
   >> that's beside the point).   
   >>   
   >> I think the lesson to learn from this thread is that a void pointer   
   >> obtained from the address of an X must not be cast back to a   
   >> pointer to some other type, even if that other type is closely   
   >> related to X.   
   >   
   > static_cast should convert the pointer if necessary,   
   > reinterpret_cast can fail.   
      
   I'm not sure I understand.  Do you mean that, for the hierarchy   
   above...   
      
     {   
       Derived d;   
       Base *bp = &d;   
       void *vp = bp;   
       Derived *dp = static_cast(vp);   
       assert(dp == &d);   
     }   
      
   ...must work?  I don't think that's guaranteed, because vp is   
   obtained from the address of a Base, and then cast back to the address   
   of a Derived.  For the hypothetical implementation that lays out the   
   Derived part before the Base part, conversions between a Base * and a   
   Derived * would need a pointer adjustment; I don't think such   
   adjustments are required to be applied when converting to or from a   
   void *.   
      
   In contrast, I would expect...   
      
     {   
       Derived d;   
       Base *bp1 = &d;   
       void *vp = bp1;   
       Base *bp2 = static_cast(vp);   
       Derived *dp = static_cast(bp2);   
       assert(dp == &d);   
     }   
      
   ...to work, because vp is obtained from a the address of a Base, and   
   then cast back to the address of a Base again. For the other   
   conversions, the compiler has all the information it needs to perform   
   any required adjustments.   
      
   I checked the standard, but I couldn't find anything that suggests   
   the first example should work.  Am I missing something?   
      
   Regards,   
      
   - Wil   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca