Re: Common base class, reinterpret_cast, and aliasing
On 27 mar, 17:58, Lance Diduck wrote:
OK to specifically get the behavior you are looking for, that does not
rely on object layout details, but only language rules:
struct C:A,B{};
int main()
{
C c;
A& a=c;
Thanks, that's a really interesting approach; it didn't occur to me to
rely on multiple inheritance. Unfortunately, it's unpractical for my
needs. I'm trying to show that C++ is powerful enough to emulate some
techniques available in modern programming languages (modern as in
almost experimental). More particularly, the example was extracted
from a case of data invariance under type convertibility.
With your approach, the class C needs to derive from all the possible
convertible types, which is to say: a potential infinity of types.
Even if we consider only the types actually present in a given
program, it still means that each object has to physically contain n
pointers (with n the number of convertible types) due to virtual
inheritance. (Anybody knows of a compiler where this kind of
inheritance comes for free?) So it wouldn't really serve as a good
example of the extendibility yet efficiency of C++ :). So, for now, I
will keep relying on free functions and reinterpret casts.
Best regards,
Guillaume
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]