public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Getting the byte offset of a member variable pointer type
@ 2005-06-22 13:48 Thomas Immich
  2005-06-22 15:55 ` Dave Korn
  0 siblings, 1 reply; 2+ messages in thread
From: Thomas Immich @ 2005-06-22 13:48 UTC (permalink / raw)
  To: gcc

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-06-22 15:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-06-22 13:48 Getting the byte offset of a member variable pointer type Thomas Immich
2005-06-22 15:55 ` Dave Korn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).