Use class template as a type

Basile B. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Nov 28 17:21:08 PST 2016


On Monday, 28 November 2016 at 14:35:36 UTC, Namespace wrote:
>> We have a handy dandy syntax for this:
>>
>> if (MyClassInt subclass = cast(MyClassInt)value) {
>> 	writeln(subclass.value);
>> }
>>
>> If it doesn't cast to said type (it will be null) that branch 
>> won't execute.
>
> Just out of interest: it looks like a dynamic_cast in C++ which 
> is considered as slow operation. Is that D cast also a dynamic 
> cast and also slow? I've never used it, so I'm a bit curious.

The cast from a class type to a sub class in itself does 
absolutely nothing. It has only an effect when you call a virtual 
method. This is slow because of the indirection that happens when 
the right offset has to be found in the VTBL.


More information about the Digitalmars-d-learn mailing list