July 21, 2004
Derek Parnell wrote:
> I was hoping to use a simple method to determine the real typeid of a
> variable at runtime using the code example below...
> 
> but this doesn't make GenClass an abstract one.
> 
> So (finally) my questions are ... Is there a better way to do this sort of
> thing, and if not, which of the two ways above is better?

You can use ClassInfo instead.  All reference types have a polymorphic classinfo reference which describes most of the important things.

 -- andy
July 21, 2004
On Tue, 20 Jul 2004 23:13:28 -0700, Andy Friesen wrote:

> Derek Parnell wrote:
>> I was hoping to use a simple method to determine the real typeid of a variable at runtime using the code example below...
>> 
>> but this doesn't make GenClass an abstract one.
>> 
>> So (finally) my questions are ... Is there a better way to do this sort of thing, and if not, which of the two ways above is better?
> 
> You can use ClassInfo instead.  All reference types have a polymorphic classinfo reference which describes most of the important things.

Hi Andy,
thanks heaps. After discovering that 'classinfo' is not documented anywhere
useful, I resorted to wading through the phobos source code to see how it
can be used. I don't understand how it works (magic comes to mind), but its
usage seems clear enough.

So now I've got this ...
<code>
 class GenClass      {char[] typename() {return this.classinfo.name;}}
 class Foo: GenClass {}
 class Bar: GenClass {}
 class XYZ: Bar      {}
 void main()
 {
    GenClass f = new Foo;
    GenClass b = new Bar;
    GenClass g = new GenClass;
    GenClass x = new XYZ;

    printf("f is a %.*s\n", f.typename);
    printf("b is a %.*s\n", b.typename);
    printf("g is a %.*s\n", g.typename);
    printf("x is a %.*s\n", x.typename);
 }
</code>

which is simple and it works.

-- 
Derek
Melbourne, Australia
21/Jul/04 5:00:14 PM
1 2
Next ›   Last »