Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Getting add...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 8 Topic 9511 of 9828
Post > Topic >>

Re: Getting address of enclosing object from address of inner object

by Lance Diduck <lancediduck@[EMAIL PROTECTED] > Apr 17, 2008 at 12:01 PM

On Apr 17, 5:58 am, xtrigger...@[EMAIL PROTECTED]
 wrote:
> my problem is the following:
> let's suppose I have a pointer to an object which I'm sure is enclosed
> in another object.
> Is it possible (safely) with some pointer arithmetics to get the
> address of the enclosing object?
This is not reliable. This may work for this particular case of a
member in a otherwise plain struct (to maintain C compatibility), but
not in general. For example, add this complexity

struct VOuter:virtual COuter{};
struct VOuter2:virtual VOuter,virtual COuter{};

So given this:
         VOuter2 outer;
         VOuter2 VOuter2 ::*membPtr( &VOuter2 ::mInner );
how would you find the address of outer from membPtr? It would be
possible to write

  COuter * outerPtr = reinterpret_cast< COuter * >
                  ( innerPtr - &( static_cast< COuter * >( 0 )->*membPtr )
);
  VOuter2 * vouterPtr=dynamic_cast<VOuter2 >(outerPtr );
however there is no way to determine just when you need that
dynamic_cast, and if you call dynamic_cast on something that has no
virtuals, well that is undefined as well.
Debuggers do use knowledge about object layout to do their work,
however there is no way in general to determine obbject layout using
the syntax of the language only

Lance


-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 8 Posts in Topic:
Getting address of enclosing object from address of inner object
xtrigger303@[EMAIL PROTEC  2008-04-17 03:58:28 
Re: Getting address of enclosing object from address of inner ob
Lance Diduck <lancedid  2008-04-17 12:01:26 
Re: Getting address of enclosing object from address of inner ob
Greg Herlihy <greghe@[  2008-04-17 16:22:29 
Re: Getting address of enclosing object from address of inner ob
courpron@[EMAIL PROTECTED  2008-04-17 16:20:23 
Re: Getting address of enclosing object from address of inner ob
xtrigger303@[EMAIL PROTEC  2008-04-18 17:16:35 
Re: Getting address of enclosing object from address of inner ob
courpron@[EMAIL PROTECTED  2008-04-19 18:15:05 
Re: Getting address of enclosing object from address of inner ob
xtrigger303@[EMAIL PROTEC  2008-04-21 10:26:26 
Re: Getting address of enclosing object from address of inner ob
courpron@[EMAIL PROTECTED  2008-04-21 16:13:17 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 15:41:07 CDT 2008.