Static cast

Frank Benoit keinfarbton at googlemail.com
Wed Jun 25 15:57:06 PDT 2008


Jason House schrieb:
> What is the proper way to implement static cast?
> 
> cast(T) to shift with an object/ interface hierarchy is dynamic and slow. My problem is that cast(T)cast(void*) does not honor vtable locations and ClassInfo is only available at runtime.

I you need this only for special classes and not as a general thing...
you can probably give those classes/interfaces a asType Method.

interface I1 {
   T asT();
}
interface I2 : I1 {
}

class A : I2 {
   T asT(){ return null; }
}

class B : A{}
class T : A{
   T asT(){ return this; }
}

Now the cast is only a virtual method call and no dynamic cast is needed 
and you get null for class which are not T.


More information about the Digitalmars-d-learn mailing list