Thread overview |
---|
March 30, 2008 Cast to a new type | ||||
---|---|---|---|---|
| ||||
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 ? |
March 31, 2008 Re: Cast to a new type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tower Ty | "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. |
Copyright © 1999-2021 by the D Language Foundation