  | 
   Simple RTTI For C++ 
   Submitted by  |   
  
  
This is some very simple RTTI (run time type information) code that you can use
for C++.  Similar to Java's instanceof operator, it allows you to take a base
pointer from some object and see if it is an instance of a derived class.
  It consists of some simple defines:
  
#define setClassname0(name) \
 virtual char *getClassname(){return name;} \
 virtual bool isInstanceOf(const char *classname)
 {return (strcmp(classname,name)==0 ? true : false);}
  #define setClassname1(name,parent) \
 virtual char *getClassname(){return name;} \
 virtual bool isInstanceOf(const char *classname)
 {return (strcmp(classname,name)==0 ? true : parent::isInstanceOf(classname));}
  #define setClassname2(name,parent1,parent2) \
 virtual char *getClassname(){return name;} \
 virtual bool isInstanceOf(const char *classname)
 {return (strcmp(classname,name)==0 ? true : (parent1::isInstanceOf(classname) || parent2::isInstanceOf(classname)));}  |  
  
 
  And say you have the following code:
 
 class Furniture{
public:
    setClassname0("Furniture");
    ...
};
  class Chair:public Furniture{
public:
    setClassname1("Chair",Furniture);
    ...
};
  class Table:public Furniture{
public:
    setClassname1("Table",Furniture);
    void doTableStuff();
    ...
};  |  
  
 
  And if you have a pointer to a Furniture, you could check to see if it is really
an instance of a Table or not, before you actually make the cast:
 
 Furniture *pFurniture=getSomeFurniture();
  if(pFurniture-isInstanceOf("Table"){
    ((Table*)pFurniture)-doTableStuff();
}   |  
  
 
  So in this case, pFurniture->isInstanceOf("Table") would return true,
pFurniture->isInstanceOf("Furniture") would also return true, but
pFurniture->isInstanceOf("Chair") would return false.
  The setClassname2 could be used if you wanted to do multiple inheritance.
  Hope this might be useful to someone.  And anyone can use this code for whatever.
 | 
 
 
 
The zip file viewer built into the Developer Toolbox made use
of the zlib library, as well as the zlibdll source additions.
 
 
 |