Group: comp.lang.c++
From: Rob
Date: Sunday, April 13, 2008 9:21 PM
Subject: Re: How would I do this dynamic_cast?

On Apr 13, 3:49=A0pm, Jim Cobban wrote:
> Rob wrote:
> > [CODE]
> > (I am not the one who defined these classes)
>
> > class _jobject {};
> > class _jarray : public _jobject {};
>
> > typedef _jobject* jobject;
> > typedef _jarray jarray;
>
> > int main()
> > {
> > =A0 =A0 =A0jobject * a;
> > =A0 =A0 =A0jarray b;
> > =A0 =A0 =A0a =3D dynamic_cast (&b);
> > ....
> > }
> > [/CODE]
>
> > This doesn't work and the problem is the only type names I'm
> > guaranteed to have are jobject and jarray. The original _jobject is
> > how it's defined here but I'm not guaranteed it''ll always be that way
> > so I have to work with jobject/jarray.
>
> > How would I cast between jobject and jarray?
>
> According to the declarations you have given jarray is a synonym for
> _jarray which is derived from _jobject, but jobject is synonym for a
> POINTER to a _jobject. =A0&b returns a pointer to the instance of _jarray,=

> which can be converted without a cast, because it is just working up the
> class hierarchy, to a pointer to _jobject, which is synonymous with
> jobject, not with pointer to jobject. =A0I do not know your objective, but=

> the following should compile:
>
> int main()
> {
> =A0 =A0 =A0 jobject a;
> =A0 =A0 =A0 jarray b;
> =A0 =A0 =A0 a =3D &b;
>
> }
>
> dynamic_cast is used for moving DOWN the class hierarchy, not up. =A0So if=

> you have a pointer to _jobject that you have reason to believe is
> actually a pointer to _jarray you can:
>
> =A0 =A0 =A0 =A0 _jobject * =A0 =A0 =A0p;
> =A0 =A0 =A0 =A0 _jarray * =A0 =A0 =A0 q;
> =A0 =A0 =A0 =A0 // no cast required because an instance of _jarray
> =A0 =A0 =A0 =A0 // is guaranteed to be an instance of _jobject
> =A0 =A0 =A0 =A0 p =A0 =A0 =A0 =3D q;
> =A0 =A0 =A0 =A0 if (Ihavereasontobelievethat_p_pointstoa_jarray) {
> =A0 =A0 =A0 =A0 // dynamic_cast required so that the type of
> =A0 =A0 =A0 =A0 // the _jobject can be verified at run time
> =A0 =A0 =A0 =A0 q =A0 =A0 =A0 =3D dynamic_cast<_jarray *>(p);
> =A0 =A0 =A0 =A0 }
>
> As I see it the problem you are facing is that the name of the typedef
> jobject is confusing because it does not warn the programmer that it is
> a pointer to a class, not a class itself. =A0If the architect had really
> intended to use the jobject and jarray definitions to hide the internal
> implementation then they should have been defined explicitly as classes,
> not as typedefs.
>
> In any event C++ pointers are extremely dangerous and should be avoided
> as much as possible. =A0They are a carryover artifact from C. =A0Their use=

> exposes you to risks because the compiler cannot catch most misuses.
> For example the compiler will permit you to use an instance of jobject
> as an array! =A0That is because C permits you to use any pointer as an
> array. =A0In your fragment of code what would happen if you coded:
>
> =A0 =A0 =A0 =A0 a[3] =A0 =A0=3D b; =A0 =A0// !?

Thanks!