Talk About Network



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 > Getting address...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 8 Topic 9511 of 9593
Post > Topic >>

Getting address of enclosing object from address of inner object

by xtrigger303@[EMAIL PROTECTED] Apr 17, 2008 at 03:58 AM

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();
}

-- 
      [ 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 Wed May 14 6:52:28 CDT 2008.