Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6572679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:02:13+00:00 2026-05-25T15:02:13+00:00

I have some code that implements a kind of run-time reflection. In order to

  • 0

I have some code that implements a kind of run-time reflection. In order to get a pointer to a field of a class inside a given instance, i basically take the pointer to the class instance and add a fixed offset that is calculated once for each field that is exposed to the reflection library.

I kept the implementation quite simple, since i didn’t need to support multiple inheritance and i made the mistake of not taking into account that, even with single inheritance, this situation is possible:

class A
{
public:
    unsigned int m_uiField;
};

class B : public A
{
    virtual void VirtualMethod()
    {
    }
};

int main()
{
    unsigned int uiOffsetA(reinterpret_cast<unsigned int>(&(reinterpret_cast<A *>(0)->m_uiField)));
    // uiOffsetA is 0 on VC9
    unsigned int uiOffsetB(reinterpret_cast<unsigned int>(&(reinterpret_cast<B *>(0)->m_uiField)));
    // uiOffsetB is 4 on VC9
}

In this case the virtual table pointer that my compiler puts at the beginning of each instance of B was offsetting by 4 bytes the fields of A.

My first idea was to do something similar to what i’m doing for the field offsets and store a single unsigned int as an offset for the base class to add to pointers to derived class instances together with the field offset. So, at initialization time i call this function one for each Derived class inheriting from a Base class:

template <typename Base, typename Derived>
unsigned int GetBaseClassOffset()
{
    Derived *pDerived(reinterpret_cast<Derived *>(4));
    Base *pBase(pDerived);
    assert(pBase >= pDerived);
    return reinterpret_cast<unsigned int>(pBase) - reinterpret_cast<unsigned int>(pDerived);
}

And everything seems to work with my tests using VC9.
But then it came to my mind that this area of C++ could be implementation dependent, and that other things like alignment could break this up.

In the end my question is:
Can i assume that fields of a base class will always be positioned at a constant positive offset relative to a pointer to a derived class instance?

Note: i am not saying “constant across all compilers”, i will use some code (eventually compiler dependent) to detect this offset at startup.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:02:13+00:00Added an answer on May 25, 2026 at 3:02 pm

    For this situation, you can use pointer-to-members:

    See it live: http://ideone.com/U4w7j

    struct A
    {
        unsigned int m_uiField;
    };
    
    struct B : A
    {
        virtual void VirtualMethod() { }
    };
    
    int main()
    {
        A instance_a;
        B instance_b;
    
        unsigned int A::*  ptrA = &A::m_uiField;
        unsigned int B::*  ptrB = &B::m_uiField;
    
        // application:
        unsigned int value = instance_a.*ptrA;
                     value = instance_b.*ptrA;
                   //value = instance_a.*ptrB; // incompatible types
                     value = instance_b.*ptrB;
    
        // also:
        A* dynamic = new B();
        value = dynamic->*ptrA; // etc
    }
    

    I suggest you also look at template metaprogramming features (part of TR1 and C++11 now:), notably the is_pod type trait:

    • http://publib.boulder.ibm.com/infocenter/comphelp/v9v111/index.jsp?topic=/com.ibm.xlcpp9.aix.doc/standlib/header_type_traits.htm
    • http://www.boost.org/doc/libs/1_47_0/libs/type_traits/doc/html/boost_typetraits/reference/is_pod.html
    • http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.1/structstd_1_1tr1_1_1is__pod.html
    • http://msdn.microsoft.com/en-us/library/bb982918(v=VS.100).aspx

    This is important because using offsetof on anything else is hazardous.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that gets the current logged in user. userID = request.session.get(_auth_user_id)
I have some code that I'd like to run on a page. My problem
I have some code that at one part will get executed a lot, and
I have some code that I absolutely must implement using goto . For example,
I have some code that sets up a dictionary with some defualt values for
I have some code that is using SyncEnumerator. As you can see here ,
I have some code that inserts a list item into a list... I then
I have some code that dynamically creates a new button through JavaScript that when
I have some code that attempts to test whether my application is running with
I have some code that does MemoryStream ms = new MemoryStream(); ... return Image.FromStream(ms);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.