instanceof in C++

Started by
19 comments, last by doctorsixstring 21 years, 10 months ago
What is the C++ equivalent of the Java keyword instanceof? I've checked out typeid(), but that function does not work just right for what I am trying to do. I want to be able to test what class an object is that was defined as a base class but instantiated as a derived class. Here is simple code showing what I want to do:
  
class CBaseClass
{
//...

};

class CDerivedClass : public CBaseClass
{
//...

}

void Main()
{
  CBaseClass *Base = new CBaseClass();
  CBaseClass *Derived = new CDerivedClass();

  if( typeid(Derived) == typeid(Base) )
    printf("classes are the same");
  else
    printf("classes are different");   //I want it to return this, because I want to know EXACTLY what class the object is (base OR derived)


}
  
[edited by - doctorsixstring on June 5, 2002 3:56:47 PM]
Advertisement
You can provide a member function which tell''s you what type it is. It just has to return a idnumber or a string.

"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
That sounds like a good idea. If no one else has any ideas on using a single keyword, I will go with that.

-Mike
You can also try a dynamic_cast to a pointer to the derived type. If it returns null, then base* doesn''t point to a derived. If you get a valid pointer, then it can be a derived

something like this...

  class base;class derived:public base;base* b=new base();base* d=new derived();if(dynamic_cast<derived*>(b))  //derivedelse   //baseif(dynamic_cast<derived*>(d))  //derivedelse  //base  

Try:
typeid(*pt).name();
sjelkjd: The compiler gives me an error, stating that my base class is not a polymorphic type.

fallenang3l: If I give typeid() a straight pointer to an object, it returns a string like this: "class CBase *". If I add the * to the parameter, it returns this: "class CBase *".

Any other ideas?


quote:Original post by doctorsixstring
sjelkjd: The compiler gives me an error, stating that my base class is not a polymorphic type.


You have to enable RTTI: Project->Settings->c++ Language

not only that, dynamic_cast only works on base classes that define at least one virtual function.
daerid@gmail.com
Perhaps most importantly, why do you need to know? Or is this just an exercise?
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
If you derive all your possible objects from an interface (say, CRTTI), then you can include a pure virtual method in that, and make all your deriving classes implement it. That method can return the type.

Although, I found my solution to be much more useful in a ''COM-style'' situation, when you want to find out if an object supports a particular interface.

Superpig
- saving pigs from untimely fates
- sleeps in a ham-mock at www.thebinaryrefinery.cjb.net

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

This topic is closed to new replies.

Advertisement