The C and C++ code snippets monologue forum

The C, C++, Code, Coder, Coding Snippets Zone for Tenouk.com

 

 
It is currently Thu Sep 09, 2010 12:55 am

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Getting the run time type information (RTTI)
PostPosted: Fri Dec 01, 2006 4:06 pm 
Offline

Joined: Sat Nov 11, 2006 2:09 am
Posts: 89
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info:
To do: -
To show: Getting the run time type information (RTTI)


Code:
// Getting the run time type information
#include <iostream>
#include <typeinfo>
using namespace std;

// polymorphic base class...
class __rtti Test
{
   // This makes Test a polymorphic class type.
   virtual void func() {};
};

// derived class...
class Derived : public Test {};

int main(void)
{
   // Instantiate Derived type object...
   Derived DerivedObj;
   // Declare a Derived type pointer
   Derived *DerivedPtr;
   // Initialize the pointer
   DerivedPtr = &DerivedObj;

   // do the run time checking...
   if(typeid(*DerivedPtr) == typeid(Derived))
      // check the type of *DerivedPtr
      cout<<"Ptr *DerivedPtr type name is "<<typeid(*DerivedPtr).name();
   if(typeid(*DerivedPtr) != typeid(Test))
      cout<<"\nPointer DerivedPtr is not a Test class type.\n";

   return 0;
}


You may continue your debug though there are errors.

Output:

Code:
c and f are different type.
int before double: 1
double before int: 0
class A before class B: 1
Press any key to continue . . .

_________________
trapped yourself at honeypot!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Skin by Lucas Kane