www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - typeof reference to class object

reply Steve Teale <steve.teale britseyeview.com> writes:
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!
Mar 19 2009
parent Gide Nwawudu <gide btinternet.com> writes:
On Thu, 19 Mar 2009 06:20:40 -0400, Steve Teale
<steve.teale 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
Mar 19 2009