Cast to a new type
Neil Vice
sardonicpresence at gmail.com
Sun Mar 30 19:37:12 PDT 2008
"Tower Ty" <tytower at yahoo.com.au> wrote in message
news:fsp4ft$7no$1 at digitalmars.com...
>I dont understand casting well. I see the following line in a program
>
> obj[0] = cast(Object) new ArrayWrapperString(data);
>
> Does ArrayWrapperString() exist in the Tango library (I did not find it)
> or does this automatically create an instance of new type and create the
> type too. ?
> Where could I read up on this ?
>
The syntax "new Class(args)" constructs a new instance of a class on the
heap. This will allocate memory for the instance and call the constructor
that takes the arguments that are supplied in the parenthises.
The syntax "cast(type)" is a unary prefix operator that takes one argument
(in this case the newly constructed ArrayWrapperString instance) and
explicitly casts in to a reference of type Type (in this case Object).
Given that all classes in D inherit implicitly from class Object and I
believe you should be able to implicitly cast an instance to a reference to
any ancestor class I'd be surprised if the explicit cast was necessary.
The ArrayWrapperString class must be defined, no automatic type-creation
occurs, and given this is from a DWT example I can only assume that it is
declared as part of that library, though as it uses Tango it could also be
in there - I'm personally familiar with neither.
If it was me I'd:
grep -R "ArrayWrapperString" *
wherever I'd put my Tango/DWT source files to look for the declaration given
that some quick google/digitalmars searching proved relatively fruitless.
Hope that puts you on the right track.
More information about the Digitalmars-d-learn
mailing list