This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: dynamic_cast problem


On Nov 13, 2007 9:44 PM, ganesh subramonian <gun_raj@yahoo.com> wrote:
> I have this example program below.
>
> class Derived: public Base {
>
> class Derived2: public Base {
>
>    Derived *d1 = dynamic_cast<Derived*>(a);
>
>    d1 = dynamic_cast<Derived*>((Derived2*)a);
>
>  Can someone tell me why the cast2 fails but cast1
> works? What effect does the cstyle cast have?
>
> thanks
> ganesh
>

From the 2003 spec 5.2.7 dynamic_cast paragraph 8:

The run-time check logically executes as follows:
— If, in the most derived object pointed (referred) to by v, v points
(refers) to a public base class subobject
of a T object, and if only one object of type T is derived from the
sub-object pointed (referred) to
by v, the result is a pointer (an lvalue referring) to that T object.
— Otherwise, if v points (refers) to a public base class sub-object of
the most derived object, and the
type of the most derived object has a base class, of type T, that is
unambiguous and public, the result
is a pointer (an lvalue referring) to the T sub-object of the most
derived object.
— Otherwise, the run-time check fails.

Neither class is a base class of each other.

corey


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]