top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Describe run-time type identification in ANSI C?

+2 votes
284 views
Describe run-time type identification in ANSI C?
posted Jan 28, 2016 by Mohammed Hussain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Static and Dynamic Types
In C++, pointers to classes have a static type, the type written in the pointer declaration, and a dynamic type, which is determined by the actual type referenced. The dynamic type of the object could be any class type derived from the static type. In the following example, ap has the static type A* and a dynamic type B*.

class A {};
class B: public A {};
extern B bv;
extern A* ap = &bv;

RTTI allows the programmer to determine the dynamic type of the pointer.

RTTI Options

In compatibility mode (-compat[=4]), RTTI support requires significant resources to implement. RTTI is disabled by default in that mode. To enable RTTI implementation and recognition of the associated typeid keyword, use the option -features=rtti. To disable RTTI implementation and recognition of the associated typeid keyword, use the option --features=no%rtti (the default).
In standard mode (the default mode), RTTI does not have a significant impact on program compilation or execution. RTTI is always enabled in standard mode.

typeid Operator

The typeid operator produces a reference to an object of class type_info, which describes the most-derived type of the object. To make use of the typeid()function, the source code must #include the header file. The primary value of this operator and class combination is in comparisons. In such comparisons, the top-level const and volatile qualifiers are ignored, as in the following example. Note that, in this example, A and B are types which have default constructors.

#include <typeinfo>
#include <assert.h>
void use_of_typeinfo( )
{ 
      A a1;
      const A a2;
      assert( typeid(a1) == typeid(a2) );
      assert( typeid(A)  == typeid(const A) );
      assert( typeid(A)  == typeid(a2) );
      assert( typeid(A)  == typeid(const A&) );
      B b1;
      assert( typeid(a1) != typeid(b1) );
      assert( typeid(A)  != typeid(B) ); 
}

The typeid operator throws a bad_typeid exception when given a null pointer.

type_info Class

The class type_info describes type information generated by the typeid operator. The primary functions provided by type_info are equality, inequality, beforeand name. From <typeinfo.h>, the definition is:

class type_info {
        public:
            virtual ~type_info( );
            bool operator==( const type_info &rhs ) const;
            bool operator!=( const type_info &rhs ) const;
            bool before( const type_info &rhs ) const;
            const char *name( ) const;
        private:
            type_info( const type_info &rhs );
            type_info &operator=( const type_info &rhs );
    };

The before function compares two types relative to their implementation-dependent collation order. The name function returns an implementation-defined, null-terminated, multibyte string, suitable for conversion and display.
The constructor is a private member function, so you cannot create a variable of type type_info. The only source of type_info objects is in the typeid operator.

answer Jan 29, 2016 by Manikandan J
Similar Questions
+4 votes

What will be the output of this code?

 void main(){
       int i=320;
       char *ptr=(char *)&i;
       printf("%d",*ptr); 
    }
+3 votes

How to print current date and time in C/C++, sample program will help?

+2 votes

I assume that constant must be initialized to a constant value at compile time, but following code the return value of ge_const() is assigned to x which will be collected at run time. So this must cause an error but I am getting the output as 50. Can someone clarify the detail?

main()
{
    const int x = get_const();
    printf("%d", x);
}
int get_const()
{
    return 50;
}
...