Topic: Constructors and runtime typing


Author: ellis@parc.xerox.com (John Ellis)
Date: 26 Jan 93 04:33:42 GMT
Raw View
Regarding Stroustrup's proposal for runtime typing:

Suppose class B is derived from A and A's constructor calls typeof(*this):

    class A    {A() {...typeof(*this)...}; ...};
    class B: A {...};

(Excuse any mistakes in notation.)

When "new B" is evaluated, will typeof(*this) yield typeof(A) or
typeof(B)?

The recommended implementation technique seems to suggest typeof(A),
and that's consistent with the handling of virtual functions within
constructors.




Author: bs@alice.att.com (Bjarne Stroustrup)
Date: 26 Jan 93 18:38:57 GMT
Raw View

ellis@parc.xerox.com (John Ellis @ Xerox PARC) writes

 > Regarding Stroustrup's proposal for runtime typing:
 >
 > Suppose class B is derived from A and A's constructor calls typeof(*this):
 >
 >     class A    {A() {...typeof(*this)...}; ...};
 >     class B: A {...};
 >
 > (Excuse any mistakes in notation.)

It is typeid() not typeof().

 > When "new B" is evaluated, will typeof(*this) yield typeof(A) or
 > typeof(B)?
 >
 > The recommended implementation technique seems to suggest typeof(A),
 > and that's consistent with the handling of virtual functions within
 > constructors.

typeid(*this)==typeid(A)

As you point out anything else would be inconsistent with the behavior
of virtual functions.

References:

 Run-Time Type Identification for \*C (Revised). Proc USENIX 1992 C++ Conference

 x3j16/92-0121, WG21/N0198: Run-Time Type Identification for \*C (Revised yet again)

Both by me and Dmitry Lenkov. The latter was revised to reflect discussions at the USENIX conference and
elsewhere.