Base class fields offset
<p>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.</p>

<p>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:</p>

<pre><code>class A
{
public:
    unsigned int m_uiField;
};

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

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

<p>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.</p>

<p>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:</p>

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

<p>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.</p>

<p>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?</p>

<p>Note: i am not saying "constant across all compilers", i will use some code (eventually compiler dependent) to detect this offset at startup.</p>
