Download presentation
Presentation is loading. Please wait.
1
Programming with ANSI C ++
A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
2
Chapter 11 Run Time Type Information and casting operators
3
Introduction RTTI is a very powerful tool in C++ for finding out the type of an object at run time. RTTI also impacts the performance of the system to a notable extent. Compilers disable RTTI by default.
4
The addition in the standard
ANSI C++ added two keywords to work with RTTI typeid returns the type of object in a specific form dynamic_cast is a new type of operator
5
Polymorphism typeinfo objects
polymorphic objects possible to be pointed to by a base class pointer and can be manipulated at run time. Virtual function has limitations
6
The Hierarchy News SendNews(); StoreNews() //PrintNews()
NewsWithPlainText NewsWithImages
7
The Solution RTTI requires polymorphic objects to operate.
The <typeinfo> library contains the definition of type_info object. Automatically created and associated with every data type used For non-polymorphic data types, it is available even when RTTI is not enabled One type_info object for one type
8
The Typeinfo Object attributes
Name() function Operator == Operator !=
9
The Hierarchy for next example
Shape Rectangle Circle Dot
10
Applying typeid to Class Names and Objects
Difference between typeid(NewsItem) and typeid (PlainTextNews),
11
Need for RTTI: case 1 and 2 Prior knowledge about usage of the class is not available to designer in some cases; and we may need to use RTTI the users of these classes are not involved in the design user needs operations other than designed Designer has no idea of all possible applications While using templates, we may need to know about the actual data type and provide validations
12
Need for RTTI : Case 3 and 4 RTTI enables the user to decide about the type of the object at run time and take decision at that point of time. We may need to do specific job for a specific subset of class hierarchy safely, then RTTI is handy
13
The problem with the solution
typeid sovlves the problem but not completely We still need to modify code when a new class is added An idea solution does not need code modification
14
Solution The operator dynamic_cast, is a solution for the problem in the previous section This casting operator is used only for polymorphic object casting. That means it can cast from one polymorphic object to another polymorphic object only
15
Features of dynamic_cast
It succeeds only when it is properly casted dynamic_cast <ToObjectPtrOrRef> (FromObjectPtrOrRef) It casts from FromObject to ToObject pointer or reference. The casting from Base pointer to Derived pointer can succeed only if base pointer is actually pointing to an object of Derived type
16
Dynamic Cast Summary Pointer to Content Casted to Successful? Base Yes
Derived No (Not Possible)
17
Using dynamic_cast to replace typeid
In some cases the code using dynamic_cast can be replaced by a code using typeid Typeid has the same performance overhead as the virtual function Performance of the result is dependent on the length of the inheritance chain
18
Const Cast It is special; no other cast, i.e. dynamic, static, or reinterpret cast can do the same The cast from const to non const can be done either with pointers or reference.
19
Two different types of const
Real const, which is defined like const int RealConstInt; Contextual const <function header> (const int *NotActuallyConst) The variable assumed constness upon entry to the function.
20
Two different types of const
Only contextual consts can be converted to non-const Real const variables like RealConstInt defined above cannot be converted to non-const
21
Static_cast Static_cast is a normal non-polymorphic cast
Converting int to double, etc. should be done using this cast If applied to a polymorphic object, static_cast results in compile time error If conversion from a data type is not relevant, static_cast again results in compile time error Static cast is performed at compile time
22
reinterpret_cast If a programmer purposefully wants to convert to an irrelevant type; (like converting an integer value to a pointer) then reinterpret_cast provides a way to do it
23
Importance of RTTI for template objects
In case of templates the type is determined at compile time and a specific function is instantiated that works for that type We can use typeid here to check the type of the item and then execute specific validation routine for that particular data type
24
Backward Compatibility with C and Casting
It is important to note that the compatibility with C is a major concern to C++ designers C style casting is still being used in a large number of programs It is not recommended to use that in newer C++ programs though.
25
Efficiency Using RTTI is not efficient
Even if we do not use typeid or dynamic_cast, when RTTI is enabled, the program runs slower
26
Cross Casting Cross casting refers to casting from derived to proper base class when there are multiple base classes in case of multiple-inheritance. Suppose we have the following class Derived : public Base1, public Base2; Base1 *Base1Ptr = new Derived; Base2 *Base2Ptr; Base2Ptr = Base1; //cross casting
27
Cross Casting Base2 Sub Object Base1 Sub Object Remaining Derived
28
Cross Casting Base1Ptr pointer’s value is incremented to make it point to Base2 Sub Object of the derived class and then assigned to the Base2Ptr pointer Base2 *Base2Ptr= dynamic_cast<Base2 *> (Base1Ptr);
29
Downcasting The cast from a base class pointer to a derived class pointer is downcasting It was considered unsafe and wrong programming before RTTI was introduced because we had to use C style casting
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.