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 32,694 of 33,346   
   Gene Bushuyev to Edmund Kapusniak   
   Re: Casting a pointer-to-member from der   
   27 Nov 12 20:57:55   
   
   From: publicfilter@gbresearch.com   
      
   On Monday, November 26, 2012 2:20:03 PM UTC-8, Edmund Kapusniak wrote:   
   > Hello,   
   >   Is this valid C++?   
   > #include    
   > class base   
   > {   
   > };   
   > class derived : public base   
   > {   
   > public:   
   >     virtual void method() { printf( "x()\n" ); }   
   > };   
   > void (base::*pmethod)() = static_cast< void (base::*)() >( &derived::method   
   );   
   > int main( int argc, char* argv[] )   
   > {   
   >     derived d;   
   >     base* b = &d;   
   >     ( b->*pmethod )();   
   >     return 0;   
   > }   
      
   I think it is legal. The section of the standard that talks about   
   this conversion is 5.2.9 verse 12. Important things here, "If class B   
   contains the original member, or is a base or derived class of the   
   class containing the original member, the resulting pointer to member   
   points to the original member. Otherwise, the result of the cast is   
   undefined. [ Note: although class B need not contain the original   
   member, the dynamic type of the object on which the pointer to member   
   is dereferenced must contain the original member; see 5.5. —end note ]"   
      
      
   --   
         [ 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