Re: Structure mapping using reinterpret_cast.
Hi,
Thank you all for these very complete answers.
In my example, I forgot to point out that class 'A' would follow
certain specific traits :
- Only POD member data.
- No virtual functions.
- No static data.
- No base class.
In the same way, class 'B' and class 'C' would also have specific
traits :
- Only derived from class 'A'
- No virtual functions.
- No member data.
I agree that the standard can sometimes contradict itself, but could
you determine that this is guaranteed to work by the following :
If give two classes 'X' and 'Y' as follows :
class X
{
/* data members, functions, etc ... */
};
class Y : public X
{
/* data members, functions, etc ... */
};
And an instance of class 'Y' that we call 'y'
Is there a guarantee that '&y == &((X &)y)' ? I know this can blow up
with multiple inheritance, and I am confining myself to the scope of
the above example. If this is true, and given that a class always has
the same memory layout, in my original example with class 'A', 'B' and
'C' :
A *a = new A(12);
B *b = reinterpret_cast<B *>(a);
C *c = reinterpret_cast<C *>(a);
You should have that 'b' and 'c' are properly mapped over 'a' as if
'a' was the instance of their base class. Anyway, I see that I'll have
to wait for C++0x to be official (and implemented) before performing
those tricks in a guaranteed way :)
Thanks for your time again,
Olivier.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]