Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

doctorsixstring

instanceof in C++

This topic is 5952 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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))
//derived

else
//base

if(dynamic_cast<derived*>(d))
//derived

else
//base


Share this post


Link to post
Share on other sites
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?


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
not only that, dynamic_cast only works on base classes that define at least one virtual function.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!