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 4 of 8 Topic 9511 of 9807
Post > Topic >>

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

by courpron@[EMAIL PROTECTED] Apr 17, 2008 at 04:20 PM

On Apr 17, 11:58 am, xtrigger...@[EMAIL PROTECTED]
 wrote:
> Hi to all,
>
> 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?
> Obviously without passing a "back pointer" to the constructor of the
> inner object...
>
> The example below seems to work but I would like some insights from
> the experts.
>
> This is just something I would use for a debugging class, in working
> code I would certainly
> pass a pointer from the enclosing class to the inner object.
> Suppose also that I want to use this trick in the CONSTRUCTOR of the
> inner object.
> At that time the outer object would not be constructed already but the
> address I get should be correct,
> right?
>
> Thanks very much in advance,
> Francesco
>
> P.S.
> Sorry for cross-posting to c.l.c++, but I got no answers...
>
> #include <iostream>
> #include <cassert>
>
> struct CInner {};
> struct COuter
> {
>          char mPad[ 10 ];
>          CInner mInner;
>
> };
>
> int main()
> {
>          COuter  outer;
>          CInner COuter::*        membPtr( &COuter::mInner );
>          std::cout << &outer << std::endl;
>          std::cout << &( outer.*membPtr) << std::endl;
>
>          // LET'S SUPPOSE I HAVE THE ADDRESS OF THE INNER OBJECT
>          CInner * innerPtr( &( outer.*membPtr ) );
>
>          // NOW I WANT TO GET THE ADDRESS OF THE ENCLOSING OBJECT
>          // IS THE FOLLOWING OK?
>          COuter * outerPtr = reinterpret_cast< COuter * >
>                  ( innerPtr - &( static_cast< COuter * >( 0 )->*membPtr
) );
>          std::cout << outerPtr << std::endl;
>
>          assert( &outer == outerPtr );
>          std::cin.get();
>

If COuter is a POD type, it will work. Otherwise it may not work, for
example when multiple and virtual inheritances are involved. By the
way you should better use the (rather unknown) offsetof macro defined
in the <cstddef> header. With the latter, you can replace this :

>          COuter * outerPtr = reinterpret_cast< COuter * >
>                  ( innerPtr - &( static_cast< COuter * >( 0 )->*membPtr
) );

by :

          COuter * outerPtr2 = reinterpret_cast< COuter * >
                  ( innerPtr - offsetof(COuter, mInner) );

which is more ****table on different architectures. Of course the C++
standard states that the first argument of the offsetof macro shall
accept a POD type.


Alexandre Courpron.


-- 
      [ 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 Sat Jul 19 20:04:51 CDT 2008.