www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - getting type of object

reply 0ffh <spam frankhirsch.net> writes:
Hi,

if I have an object of class Foo, or a subclass of Foo, like:

   Foo foo=someInstanceOfFooOrItsSubclasses();

Is there a more elegant method to get the type of the object
than adding a member function to Foo and all its subclasses
that does something like:

   TypeInfo getType()
   {
     return typeid(typeof(this));
   }

?

Regards, Frank
Nov 11 2007
parent reply 0ffh <spam frankhirsch.net> writes:
May I "if (theInstance.classinfo==TheClass.classinfo)"?

Seems to work...
Nov 11 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
0ffh wrote:
 
 May I "if (theInstance.classinfo==TheClass.classinfo)"?
 
 Seems to work...

Does the comment about Typeinfo and Polymorphism help you at all? http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/Phobos/Object --bb
Nov 11 2007
parent reply 0ffh <frank frankhirsch.youknow.what.todo.net> writes:
Bill Baxter wrote:
 0ffh wrote:
 May I "if (theInstance.classinfo==TheClass.classinfo)"?

 Seems to work...

Does the comment about Typeinfo and Polymorphism help you at all? http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/Phobos/Object

I'll take that as a "yes"... =) I was mainly concerned here if using the classinfo member counts as a hackish OMGWhatHaveYouGoneAndDoneNow??? or if it's okay to. Thanks, Frank
Nov 11 2007
parent Bill Baxter <dnewsgroup billbaxter.com> writes:
0ffh wrote:
 Bill Baxter wrote:
 0ffh wrote:
 May I "if (theInstance.classinfo==TheClass.classinfo)"?

 Seems to work...

Does the comment about Typeinfo and Polymorphism help you at all? http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/Phobos/Object

I'll take that as a "yes"... =) I was mainly concerned here if using the classinfo member counts as a hackish OMGWhatHaveYouGoneAndDoneNow??? or if it's okay to. Thanks, Frank

That sounds ok. If you want to see hackish uses of classinfo go visit the source code for std.boxer. --bb
Nov 11 2007