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 6 of 8 Topic 9511 of 9775
Post > Topic >>

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

by courpron@[EMAIL PROTECTED] Apr 19, 2008 at 06:15 PM

On Apr 19, 1:16 am, xtrigger...@[EMAIL PROTECTED]
 wrote:
> Thanks a lot to all.
>
> I got Lance point.
> But I just want to get the address of the first enclosing object.
> I'm not interested  in the dynamic type of an object that may have
> derived from this "first encloser".
> ( Hope my english is good enough ).


If you don't care at all about the dynamic type of the object, then it
may work in practice.


> PLUS, I do not understand why this technique should not work with non
> POD types.

It ultimately depends on the compiler. For example, popular
implementations of multiple and virtual inheritance involve member
reordering. Consider the following example :


#include <cassert>
#include <cstddef>

struct COuter {
    char mPad1[ 5 ];
    int mInner;
    char mPad2[ 5 ];
};

struct Base : public virtual COuter {
    int b;
};

struct Derived :public Base, public virtual COuter {
    int d;
};

void* GetEnclAddrFromInner ( int * inner  )
{
    return (void*)  ( (char*)inner -
                      offsetof(COuter, mInner)  );
}


void CheckPointers ( void* true_ptr, COuter * obj ) {
    assert( true_ptr == GetEnclAddrFromInner( &obj->mInner ) );
}

int main()
{
    COuter a;
    Derived b;

    CheckPointers( (void*)&a, &a ); #1
    CheckPointers( (void*)&b, &b ); #2
}

Line #1 will probably be ok, while line #2 probably won't pass the
assertion, depending on the compiler.


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 Tue Jul 8 23:23:39 CDT 2008.