typeof reference to class object

Gide Nwawudu gide at btinternet.com
Thu Mar 19 04:15:30 PDT 2009


On Thu, 19 Mar 2009 06:20:40 -0400, Steve Teale
<steve.teale at britseyeview.com> wrote:

>import std.stdio;
>
>class A
>{
>   int a;
>}
>
>class B
>{
>   int b;
>}
>
>void main()
>{
>   int n;
>   Object a = new A;
>   Object b = new B;
>
>   writefln("%s %s %s", typeof(n).stringof, typeof(a).stringof, typeof(b).stringof);
>}
>
>prints int Object, Object.
>
>This seems somewhat counter-intuitive for an object oriented language!

typeof resolves at compile time, if you want the actual class name you
should use classinfo.

import std.stdio;

class A
{
   int a;
}

class B
{
   int b;
}

void main()
{
   int n;
   Object a = new A;
   Object b = new B;

   writefln("%s %s %s", typeof(n).stringof, 
                        a.classinfo.name, b.classinfo.name);
}


Gide



More information about the Digitalmars-d mailing list