March 19, 2009 typeof reference to class object | ||||
|---|---|---|---|---|
| ||||
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!
| ||||
March 19, 2009 Re: typeof reference to class object | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply