From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8099 invoked by alias); 22 Jun 2005 13:48:46 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 8086 invoked by uid 22791); 22 Jun 2005 13:48:39 -0000 Received: from moutng.kundenserver.de (HELO moutng.kundenserver.de) (212.227.126.188) by sourceware.org (qpsmtpd/0.30-dev) with ESMTP; Wed, 22 Jun 2005 13:48:39 +0000 Received: from ergosign0.science.salink.net [212.18.198.73] (helo=[192.168.0.33]) by mrelayeu.kundenserver.de with ESMTP (Nemesis), id 0MKwpI-1Dl5an1GHV-0003Qv; Wed, 22 Jun 2005 15:48:37 +0200 Mime-Version: 1.0 (Apple Message framework v730) Content-Transfer-Encoding: 7bit Message-Id: <311FCBFD-7462-42C9-8701-5869B3AC37BE@ergosign.de> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed To: gcc@gcc.gnu.org From: Thomas Immich Subject: Getting the byte offset of a member variable pointer type Date: Wed, 22 Jun 2005 13:48:00 -0000 X-SW-Source: 2005-06/txt/msg00968.txt.bz2 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(&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