typeof()
J Arrizza
cppgent0 at gmail.com
Thu Oct 20 06:46:09 PDT 2011
Maybe I'm doing something else incorrectly:
class Base
{
public void inBase()
{
}
}
class Bob : Base
{
public void inBob()
{
}
}
Bob bob = new Bob();
writeln("Bob as Bob");
foreach (i, m; __traits(allMembers, typeof(bob)))
{
writeln("=== i=", i, " m=", m);
}
Base base = bob;
writeln("Bob as Base");
foreach (i, m; __traits(allMembers, typeof(base)))
{
writeln("=== i=", i, " m=", m);
}
The output is:
Bob as Bob
=== i=0 m=inBob
=== i=1 m=inBase
=== i=2 m=toString
=== i=3 m=toHash
=== i=4 m=opCmp
=== i=5 m=opEquals
=== i=6 m=Monitor
=== i=7 m=factory
Bob as Base
=== i=0 m=inBase ; missing inBob()
=== i=1 m=toString
=== i=2 m=toHash
=== i=3 m=opCmp
=== i=4 m=opEquals
=== i=5 m=Monitor
=== i=6 m=factory
If typeof(base) returned Bob as the type, then the two lists of members
should be identical. Or am I missing something else?
If I use derivedMembers instead of allMembers, the output is:
Bob as Bob
=== i=0 m=inBob
Bob as Base
=== i=0 m=inBase
which still seems to support that typeof() doesn't return the underlying
type.
John
On Thu, Oct 20, 2011 at 12:25 AM, Jens Mueller <jens.k.mueller at gmx.de>wrote:
> J Arrizza wrote:
> > typeof returns the type of the object given to it:
> >
> > SomeClass sc;
> > typeof(sc) // returns SomeClass
> >
> > Object o = sc;
> > typeof(o) // returns Object
> >
> > Is there a way or call to get the underlying type?:
> >
> > typeof2(o) //returns SomeClass
> >
> > I checked the online doc, but nothing in the Declarations section that I
> > could see.
>
> typeid should work.
> http://d-programming-language.org/expression.html#TypeidExpression
>
> Jens
>
--
John
blog: http://arrizza.blogspot.com/
web: http://www.arrizza.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20111020/e2b138ed/attachment-0001.html>
More information about the Digitalmars-d
mailing list