how to determine a runtime type faster?

Vladimir Panteleev thecybershadow at gmail.com
Thu May 10 06:46:55 PDT 2007


On Thu, 10 May 2007 16:16:42 +0300, davidl <davidl at 126.com> wrote:

> module funky.super.meaningless.modulename;
> class base
> {
> }
> class derive:base
> {
> }
> void main()
> {
>      derive instance= new derive;
>      base castedinstance=instance;
>      // notice the following would result a super long string comparision ,
> which is not so good.
>      //
> `funky.super.meaningless.modulename.derive`==`funky.super.meaningless.modulename.derive`
>      assert(castedinstance.classinfo.name==derive.classinfo.name);
>
> }
> the idea is castedinstance.classinfo.runtimeid?
> the runtimeid could generated by the compiler and with some proper design
> about object.d , i think
> the comparision could be faster

 From http://www.digitalmars.com/d/expression.html#CastExpression :

> Any casting of a class reference to a derived class reference is done with a runtimecheck to make sure it really is a downcast. null is the result if it isn't. Note: Thisis equivalent to the behavior of the dynamic_cast operator in C++.

So, for your example you only need:

assert(cast(derive)castedinstance);

-- 
Best regards,
  Vladimir                          mailto:thecybershadow at gmail.com



More information about the Digitalmars-d mailing list