GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 53 of 203    Print  
What is a "RTTI"?

  
Total Answers and Comments: 4 Last Update: April 21, 2006     Asked by: suji 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: mohit12379
 

Run-time type information (RTTI) is a mechanism that allows the type of an object to be determined during program execution. RTTI was added to the C++ language because many vendors of class libraries were implementing this functionality themselves. This caused incompatibilities between libraries. Thus, it became obvious that support for run-time type information was needed at the language level.

There are three main C++ language elements to run-time type information:

1)The dynamic_cast operator. :- Used for conversion of polymorphic types.
 
  class B { ... };
  class C : public B { ... };
  class D : public C { ... };
 
  void f(D* pd)
  {
     C* pc = dynamic_cast<C*>(pd);// ok: C is a direct base class
                                  // pc points to C subobject of pd
 
     B* pb = dynamic_cast<B*>(pd);// ok: B is an indirect base class
                                  // pb points to B subobject of pd
     ...
  }
---------------------------------------------------------------------------
2)The typeid operator :- Used for identifying the exact type of an object.
                       
3)The type_info class :- Used to hold the type information returned by the typeid operator.
                         The type_info class describes type information generated within the
                         program by the compiler.
 
NOTE:-   TRY THIS EXAMPLE IS COMPILE AND LINK USING MSVC++ 6.0 CL.EXE Compiler and Linker PLS CHECK SAME typid available in UNIX based C++;
 **************************************************************************************                       
 #include "stdio.h"
 #include <typeinfo.h>
 
 class A
 {
 public:
 };
 
 class B:public A
 {
 public:
 };
 
 int main(int argc, char* argv[])
 {
    int   iVal = int();
    float fVal = float();
    char  cVal = char();
    A a,a1;
    B b;
 

    const type_info& t_iVal = typeid(iVal);   // Holds Simple int DataType Info
    const type_info& t_iValRef = typeid(&iVal); // Holds pointer type_info


    printf("\n Type Info of  iVal = %s\n",t_iVal.name());
    printf("\n Type Info of &iVal = %s\n",t_iValRef.name());
 
    printf("\n Type Info of fVal = %s\n",typeid(fVal).name());
    printf("\n Type Info of &fVal = %s\n",typeid(&fVal).name());
 
    printf("\n Type Info of cVal = %s\n",typeid(cVal).name());
    printf("\n Type Info of &cVal = %s\n\n",typeid(&cVal).name());
 
    printf("\n Type Info of a = %s\n",typeid(a).name());
    printf("\n Type Info of b = %s\n\n",typeid(b).name());
 
    if(typeid(a) == typeid(a1))
    {
     printf("\n BOTH INSTANCES a AND a1 BELONGS TO SAME CLASS \n\n");
    }
 
    return 0;
}
****************************************************************************
                         
 PLS ANY  DOUBT WRITE ME AT mohit.gonduley@gmail.com



Above answer was rated as good by the following members:
wael.salman
September 20, 2005 01:15:52   #1  
kulwant1977 Member Since: September 2005   Contribution: 7    

RE: What is a "RTTI"?
RTTI as known Runtime Type Identification.
If you want to cast the object of one class as the object of another class so u require to know the type of the object at the runtime and u may want to cofirm is tht object of the same class you want to change we use the typeid operator to check what is the objects type and then use dynamic cast to cast the object.



 
Is this answer useful? Yes | No
January 02, 2006 02:29:27   #2  
apiplani Member Since: December 2005   Contribution: 11    

RE: What is a "RTTI"?

Does this mean that RTTI is the term used for resolution of the function at the run time. As far as I understand in case of virutal functions as well the compiler resolves at the run time the exact function to be used. Will this resolution be also termed as RTTI??????


 
Is this answer useful? Yes | No
January 28, 2006 13:54:45   #3  
A        

RE: What is a "RTTI"?

In case of Virtual function we are responsible for deciding the function at run time. Its our logic that reflects and no one does any thing.

Where as in case of RTTI TYPE is found of object. We dont do any thing for that. We simply use that.


 
Is this answer useful? Yes | No
April 20, 2006 15:43:17   #4  
mohit12379 Member Since: March 2006   Contribution: 17    

RE: What is a "RTTI"?

Run-time type information (RTTI) is a mechanism that allows the type of an object to be determined during program execution. RTTI was added to the C++ language because many vendors of class libraries were implementing this functionality themselves. This caused incompatibilities between libraries. Thus it became obvious that support for run-time type information was needed at the language level.

There are three main C++ language elements to run-time type information:

1)The dynamic_cast operator. :- Used for conversion of polymorphic types.

class B { ... };
class C : public B { ... };
class D : public C { ... };

void f(D* pd)
{
C* pc dynamic_cast<C*>(pd);// ok: C is a direct base class
// pc points to C subobject of pd

B* pb dynamic_cast<B*>(pd);// ok: B is an indirect base class
// pb points to B subobject of pd
...
}
---------------------------------------------------------------------------
2)The typeid operator :- Used for identifying the exact type of an object.

3)The type_info class :- Used to hold the type information returned by the typeid operator.
The type_info class describes type information generated within the
program by the compiler.

NOTE:- TRY THIS EXAMPLE IS COMPILE AND LINK USING MSVC++ 6.0 CL.EXE Compiler and Linker PLS CHECK SAME typid available in UNIX based C++;
**************************************************************************************
#include stdio.h
#include <typeinfo.h>

class A
{
public:
};

class B:public A
{
public:
};

int main(int argc char* argv[])
{
int iVal int();
float fVal float();
char cVal char();
A a a1;
B b;

const type_info& t_iVal typeid(iVal); // Holds Simple int DataType Info
const type_info& t_iValRef typeid(&iVal); // Holds pointer type_info


printf( \n Type Info of iVal s\n t_iVal.name());
printf( \n Type Info of &iVal s\n t_iValRef.name());

printf( \n Type Info of fVal s\n typeid(fVal).name());
printf( \n Type Info of &fVal s\n typeid(&fVal).name());

printf( \n Type Info of cVal s\n typeid(cVal).name());
printf( \n Type Info of &cVal s\n\n typeid(&cVal).name());

printf( \n Type Info of a s\n typeid(a).name());
printf( \n Type Info of b s\n\n typeid(b).name());

if(typeid(a) typeid(a1))
{
printf( \n BOTH INSTANCES a AND a1 BELONGS TO SAME CLASS \n\n );
}

return 0;
}
****************************************************************************

PLS ANY DOUBT WRITE ME AT mohit.gonduley@gmail.com


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape