Sie sind auf Seite 1von 4

What is RTTI in C++ ?

Runtime Type Information (RTTI) is the concept of determining the type of any variable during execution (runtime.) The RTTI mechanism contains:

The operator dynamic_cast The operator typeid The struct type_info

RTTI can only be used with polymorphic types. This means that with each class you make, you must have at least one virtual function The dynamic_cast can only be used with pointers and references to objects. It makes sure that the result of the type conversion is valid and complete object of the requested class.
// dynamic_cast #include #include using namespace std; class Base_Class { virtual void dummy() {} }; class Derived_Class: public Base_Class { int a; }; int main () { try { Base_Class * ptr_a = new Derived_Class; Base_Class * ptr_b = new Base_Class; Derived_Class * ptr_c; ptr_c = dynamic_cast< Derived_Class* >(ptr_a); if (ptr_c ==0) cout << "Null pointer on first type-cast" << endl; ptr_c = dynamic_cast< Derived_Class* >(ptr_b); if (ptr_c ==0) cout << "Null pointer on second type-cast" << endl; } catch (exception& my_ex) {cout << "Exception: " << my_ex.what();} return 0; }

There are two dynamic_casts from pointer objects of type Base_Class* (namely ptr_a and ptr_b) to a pointer object of type Derived_Class*. If everything goes well then the first one should be successful and the second one will fail. The pointers ptr_a and ptr_b are both of the type Base_Class. The pointer ptr_a points to an object of the type Derived_Class. The pointer ptr_b points to an object of the type Base_Class. So when the dynamic type cast is performed then ptr_a is pointing to a full object of class Derived_Class, but the pointer ptr_b points to an object of class Base_Class. This object is an incomplete object of class Derived_Class; thus this cast will fail!

bad_cast exception is being thrown in case of conversion fail. Typeid return an structure of type_info . You can get the detail and type of the object at run time.
int * a; int b; a=0; b=0; if (typeid(a) != typeid(b)) { cout << "a and b are of different types:\n"; cout << "a is: " << typeid(a).name() << '\n'; cout << "b is: " << typeid(b).name() << '\n'; }

Posted by vikas at 6:50 PM 2 comments

C++ Resources and Interview Questions


Below are few links on C++ Resources which can help you on your interview preparation Free C++ Books Free C Books C++ Interview Questions C++ Questions Interview Questions Posted by vikas at 6:46 PM 0 comments

What is vprt, v-table and how virtual function works


Whenever a program has a virtual function declared, a v - table is constructed for the class. The v-table consists of addresses to the virtual functions for classes that contain one or more virtual functions. The object of the class containing the virtual function contains a virtual pointer that points to the base address of the virtual table in memory. Whenever there is a virtual function call, the v-table is used to resolve to the function address. An object of the class that contains one or more virtual functions contains a virtual pointer called the vptr at the very beginning of the object in the memory. Hence the size of the object in this case increases by the size of the pointer. This vptr contains the base address of the virtual table in memory. Note that virtual tables are class specific, i.e., there is only one virtual table for a class irrespective of the number of virtual functions it contains. This virtual table in turn contains the base addresses of one or more virtual functions of the class. At the time when a virtual function is called on an object, the vptr of that object provides the base address of the virtual table for that class in memory. This table is used to resolve the function call as it contains the addresses of all the virtual functions of that class Posted by vikas at 11:13 AM 1 comments

Virtual Constructors and Virtual Destructors in C++

A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor. A virtual destructor is one that is declared as virtual in the base class and is used to ensure that destructors are called in the proper order. It is to be remembered that destructors are called in the reverse order of inheritance. If a base class pointer points to a derived class object and we some time later use the delete operator to delete the object, then the derived class destructor is not called Posted by vikas at 11:10 AM 0 comments

UpCasting and DownCasting in C++


Upcasting - UpCasting is defined as casting a pointer or reference of a derived class type to a type of a pointer or reference to a base class. In case of virtual function implementation we can cast a derived class pointer or reference to base class Downcasting is defined as casting a pointer or reference of a base class type to a type of a pointer or reference to a derived class. Mostly used to restore the object to its original type. Downcasting can lead to trouble due to contravariance, e.g.: struct Base { int i_; virtual int foo (void) { return i_; } }; struct Derived : public Base { int j_; virtual int foo (void) { return j_; } }; void foo (void) { Base b; Derived d; Base *bp = &d; // "OK", a Derived is a Base Derived *dp = &b;// Error, a Base is not necessarily a Derived } Problem: what happens if dp->j_ is referenced or set? Since a Derived object always has a Base part certain operations are ok: bp = &d; bp->i_ = 10; bp->foo (); // calls Derived::foo ();

Since base objects dont have subclass data some operations arent ok e.g., accesses information beyond end of b: dp = (Derived *) &b; dp->j_ = 20; // big trouble! C++ permits contravariance if the programmer explicitly casts, e.g., dp = (Derived *) &b; // unchecked cast RTTI can be used to check the type of the object if (Derived *dp = dynamic_cast(&b)) /* use dp */; else /* error! */ For a dynamic cast to succeed, the actual type of b would have to either be a Derived object or some subclass of Derived dynamic_cast throw bad_cast exception in case of failure if the types do not match the operation fails at run-time Posted by vikas at 10:28 AM 3 comments

What is the difference between overloading and overriding in C++


This is one of the most common question asked in C++ interview. Overloading : Function overloading ( No change in input parameter or argument) Overriding : Virtual Function ( When derived class override the base class implementation)

Das könnte Ihnen auch gefallen