public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Thomas Immich <immich@ergosign.de>
To: gcc@gcc.gnu.org
Subject: Getting the byte offset of a member variable pointer type
Date: Wed, 22 Jun 2005 13:48:00 -0000	[thread overview]
Message-ID: <311FCBFD-7462-42C9-8701-5869B3AC37BE@ergosign.de> (raw)

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


             reply	other threads:[~2005-06-22 13:48 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-06-22 13:48 Thomas Immich [this message]
2005-06-22 15:55 ` Dave Korn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=311FCBFD-7462-42C9-8701-5869B3AC37BE@ergosign.de \
    --to=immich@ergosign.de \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).