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! ]