Topic: dynamic_cast of unrelated/possibly unrelated classes


Author: "Radoslav Getov" <nospam@mai.com>
Date: Tue, 10 Jul 2001 16:24:20 GMT
Raw View
"Carl Daniel" <carl@pixami.com> wrote in message
news:9id3qm$ev5@dispatch.concentric.net...
:
: > I.e. is it legal to try dynamic casts between possibly (but maybe not)
: > unrelated types?
: >
:
: Absolutely legal & defined to return null.
:
: 5.2.7 (9) "The value of a failed cast to pointer type is the null pointer
: value of the required result type.  A failed cast to reference type throws
: bad_cast (18.5.2)."
:
: -cd
:
:
Thanks
Radoslav


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: bill@gibbons.org (Bill Gibbons)
Date: Wed, 11 Jul 2001 17:00:14 GMT
Raw View
In article <KhKHkVAnWiS7Ewnh@ntlworld.com>, Francis Glassborow
<francisG@robinton.demon.co.uk> wrote:

> In article <tkjmdue7qsb58d@corp.supernews.com>, Radoslav Getov
> <nospam@mai.com> writes
> >Is the following a well defined program and/or behaviour?
> >
> >x = dynamic_cast <X*> (y);
> >
> >I.e. is it legal to try dynamic casts between possibly (but maybe not)
> >unrelated types?
>
> I believe so. The reason is that I could write:
>
> class Z: public X, public Y {}
>
> and now the cross cast is valid...

Maybe; it still depends on the complete type.

Will the following function return a non-null result given a non-null
argument?

    X* CastToX(Z* z) {
        Y* y = z;
        X* x = dynamic_cast<X*>(y);
        return x;
    }

Answer: not necessarily.  It depends on the complete (dynamic) type
of the argument.

Suppose the complete type is:

   class W : public Z, public X { };

The call "CastToX(new W)" will return null because there are two
base class subobjects of type "X" in the complete object.

I consider this a serious design flaw in dynamic_cast, and I proposed a
correction (including an example of an efficient implementation design).
But the issue was considered too obscure to be worth addressing.
(Or perhaps the title "Local Disambiguation of Dynamic Cross Casts" was
just too long.)

-- Bill Gibbons

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Radoslav Getov" <nospam@mai.com>
Date: Mon, 9 Jul 2001 16:25:59 GMT
Raw View
Hi,

Privided that:

class X
{
    virtual ~X() {}
};

class Y
{
    virtual ~Y() {}
};


X* x; // don't mind the lack of initialization - that's not the question
Y* y;

Is the following a well defined program and/or behaviour?

x = dynamic_cast <X*> (y);

I.e. is it legal to try dynamic casts between possibly (but maybe not)
unrelated types?

Thanks
Radoslav Getov



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Carl Daniel" <carl@pixami.com>
Date: Mon, 9 Jul 2001 20:28:30 GMT
Raw View
"Radoslav Getov" <nospam@mai.com> wrote in message
news:tkjmdue7qsb58d@corp.supernews.com...
> Hi,
>
> Privided that:
>
> class X
> {
>     virtual ~X() {}
> };
>
> class Y
> {
>     virtual ~Y() {}
> };
>
>
> X* x; // don't mind the lack of initialization - that's not the question
> Y* y;
>
> Is the following a well defined program and/or behaviour?
>
> x = dynamic_cast <X*> (y);
>
> I.e. is it legal to try dynamic casts between possibly (but maybe not)
> unrelated types?
>

Absolutely legal & defined to return null.

5.2.7 (9) "The value of a failed cast to pointer type is the null pointer
value of the required result type.  A failed cast to reference type throws
bad_cast (18.5.2)."

-cd



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Mon, 9 Jul 2001 21:41:16 GMT
Raw View
In article <tkjmdue7qsb58d@corp.supernews.com>, Radoslav Getov
<nospam@mai.com> writes
>Is the following a well defined program and/or behaviour?
>
>x = dynamic_cast <X*> (y);
>
>I.e. is it legal to try dynamic casts between possibly (but maybe not)
>unrelated types?

I believe so. The reason is that I could write:


class Z: public X, public Y {}

and now the cross cast is valid, so how do I check whether an X* can be
cross cast to a Y* (because the dynamic type is a Z*)? Note that any two
classes can be related this way (in theory). The only requirement is
that the classes are polymorphic so that RTTI is available.


Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]