This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Getting the byte offset of a member variable pointer type


Hi all,

I need to get the exact byte offset of a member variable inside a class in order to map a member variable pointer type to a concrete member variable instance. This worked fine until I ported my code to GCC 4.0, which is much stricter regarding type casts.

Unfortunately I cannot use the offsetof() macro, since I do not have enough information to do so.
The only information I have is:
- The member variable type (in template parameter T, for example int)
- A member variable pointer (m_memberVariablePtr, for example int MyClass::*)
- The class type (in template parameter C, for example MyClass)
- A concrete instance of type C (obj)


I want to get
- The address of obj's concrete member variable (represented by m_memberVariablePtr)


Here is how I calculated this address in GCC 3.0:

const C* addressOfObject = static_cast<const C*>(&obj);
T* result = (T*)((std::size_t)addressOfObject + (size_t) m_memberVariablePtr);


However, this snippet does not compile in GCC 4.0, since I am not allowed to cast a member variable pointer to a size_t type. Is there any elegant possibility to get the address correctly?

Thanks in advance,

Thomas



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]