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


|