[Issue 1563] dynamic cast is not always performed

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Nov 8 04:42:34 PST 2007


http://d.puremagic.com/issues/show_bug.cgi?id=1563


smjg at iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg at iname.com




------- Comment #4 from smjg at iname.com  2007-11-08 06:42 -------
(In reply to comment #3)
> is their any reson to cast somthing that isn't a C to type C? Only a few 
> reasons some to mind and I'm not shrue any of them should be allowed

It's the well-documented method of testing an object to see if it's of a
particular class.

http://www.digitalmars.com/d/expression.html#CastExpression

"In order to determine if an object o is an instance of a class B use a cast:

    if (cast(B) o)
    {
        // o is an instance of B
    }
    else
    {
        // o is not an instance of B
    }"

However, for this to work, o has to be declared as some superclass of B, not B
itself.  If the variable is declared as a B, then the compiler is within its
right to assume that the object is a B (or o is already null).

So it's reasonable that given
    C c;
then
    c = cast(C) c;
is a nop.  The problem is that, unless I've missed something, the behaviour of
casting a void* to class type is unspecified.


-- 



More information about the Digitalmars-d-bugs mailing list