Topic: Downcast and Reverse Offset


Author: shang@corp.mot.com (David (Lujun) Shang)
Date: Thu, 10 Sep 92 01:58:57 GMT
Raw View
The topic has been discussed sometime before.

I got an email from someone to point out to me that reverse offset is not
required for downcast. And I replied to agreed to this point of view at
that time. But thinking over the problem today, I find that reverse offset
is still necessary.

[I'm sorry I lost your email so I cannot reply to you directly.]

Consider the following example:

class A {};
class B1:A {};
class B2:A {};
class C: B1,B2 {};

C c;
B1 * b1p;
B2 * b2p;
A * ap1, * ap2;
C * cp1, * cp2;

b1p = &c;
b2p = &c;
ap1 = b1p;   // ap1 is pointing to the B1::A base of the object c
ap2 = b2p;   // ap2 is pointing to the B2::A base of the object c

The problem is that how the compiler knows which base of the object the base
pointer is pointing to:

cp1 = (C*) ap1;
cp2 = (C*) ap2;

Therefore, reverse offset is still necessary.

It is quite ridiculous that BC++ V3.1 does not allow the type cast in

cp1 = (C*) ap1;
cp2 = (C*) ap2;

where both ap1 and ap2 are actually pointing to an object of C! On the other
hand, cast from an arbitrary class is allowed:

cp1 = (AnyOtherClass1 *) anyOtherObjectPointer1;
cp2 = (AnyOtherClass2 *) anyOtherObjectPointer2;

???!!!

David Shang