www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Cast to a new type

reply Tower Ty <tytower yahoo.com.au> writes:
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 ?
Mar 30 2008
parent reply "Neil Vice" <sardonicpresence gmail.com> writes:
"Tower Ty" <tytower yahoo.com.au> wrote in message 
news:fsp4ft$7no$1 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.
Mar 30 2008
parent Tower Ty <towerty msn.com.au> writes:
Thanks Neil
I found it as a alias added later
module dwt.dwthelper.utils;
Mar 30 2008