Introduction to RTTI. Jonathan Hoyle Eastman Kodak 11/30/00

Similar documents
POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

Polymorphism. Zimmer CSCI 330

OBJECT ORIENTED PROGRAMMING USING C++

C++ Programming: Polymorphism

Dr. Md. Humayun Kabir CSE Department, BUET

Increases Program Structure which results in greater reliability. Polymorphism

Tokens, Expressions and Control Structures

These new operators are intended to remove some of the holes in the C type system introduced by the old C-style casts.

04-24/26 Discussion Notes

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

C++ Casts and Run-Time Type Identification

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Lecture 05 POINTERS 1

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

Inclusion Polymorphism

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

Chapter 15 - C++ As A "Better C"

UNIT - IV INHERITANCE AND FORMATTED I/O

IS 0020 Program Design and Software Tools

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

CS OBJECT ORIENTED PROGRAMMING

COMP322 - Introduction to C++

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Pointers, Dynamic Data, and Reference Types

Dynamic Allocation of Memory

CS3157: Advanced Programming. Outline

CS 162, Lecture 25: Exam II Review. 30 May 2018

TPF Users Group Spring 2005

G52CPP C++ Programming Lecture 15

G52CPP C++ Programming Lecture 20

Introduction to C++ Systems Programming

C Pointers. 7.2 Pointer Variable Definitions and Initialization

COMP 2355 Introduction to Systems Programming

ANSI C Changes. Jonathan Hoyle Eastman Kodak 10/5/00

Variables. Data Types.

What does it mean by information hiding? What are the advantages of it? {5 Marks}

C++_ MARKS 40 MIN

September 10,

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

Polymorphism. Arizona State University 1

Object-Oriented Principles and Practice / C++

W3101: Programming Languages C++ Ramana Isukapalli

a data type is Types

Dynamic Binding C++ Douglas C. Schmidt

void fun() C::C() // ctor try try try : member( ) catch (const E& e) { catch (const E& e) { catch (const E& e) {

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

G52CPP C++ Programming Lecture 16

AN OVERVIEW OF C++ 1

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

1d: tests knowing about bitwise fields and union/struct differences.

The University of Nottingham

Exception Handling in C++

Pointers, Pointers, Pointers!

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

Divyakant Meva. Table 1. The C++ keywords

C for C++ Programmers

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

CS201- Introduction to Programming Current Quizzes

CSE 333 Lecture C++ final details, networks

! Errors can be dealt with at place error occurs

Pointers and Strings Prentice Hall, Inc. All rights reserved.

C++ Crash Kurs. Exceptions. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

CA31-1K DIS. Pointers. TA: You Lu

C++ Exception Handling. Dr. Md. Humayun Kabir CSE Department, BUET

Laboratorio di Tecnologie dell'informazione

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

Absolute C++ Walter Savitch

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

CS105 C++ Lecture 7. More on Classes, Inheritance

Programsystemkonstruktion med C++: Övning 2. Karl Palmskog september 2010

EMBEDDED SYSTEMS PROGRAMMING More About Languages

Assignment 4: Semantics

CPSC 427: Object-Oriented Programming

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Where do we stand on inheritance?

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

The University of Nottingham

CSci 1113 Final. Name: Student ID:

Before we dive in. Preprocessing Compilation Linkage

C++ Inheritance II, Casting

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Name. CPTR246 Spring '17 (100 total points) Exam 2

VIRTUAL FUNCTIONS Chapter 10

Transcription:

Introduction to RTTI Jonathan Hoyle Eastman Kodak 11/30/00

Overview What is RTTI? typeid( ) function Polymorphism dynamic_cast< > operation RTTI Gotcha s Demo

What is RTTI? Run-Time Type Identification Dynamically determining an object s type Two RTTI operations: typeid( ) function (generic types) dynamic_cast< > (polymorphic types) Useful for type specialization in code Very useful for templates Good debugging tool

typeid( ) function Returns a type_info describing that type typeid() can be used on any variable or type name() returns the type name as a string type_info s can be compared using the == and!= operators It is polymorphic-friendly Must #include the following header: #include <typeinfo.h>

typeid( ) Example #1 // Prints the type name to screen template <class T> void WhatAmI(T x) { cout << typeid(x).name() << endl; } WhatAmI(1); // prints int WhatAmI(1.0); // prints double WhatAmI( Hi! ); // prints char * MyClass x; WhatAmI(x); // MyClass (CW & Borland) // or class MyClass (VC++)

typeid( ) Example #2 // Test if a given object is a basic numeric type template <class T> bool IsNumericType(T x) { if (typeid(x) == typeid(short)) return true; if (typeid(x) == typeid(long)) return true; if (typeid(x) == typeid(int)) return true; if (typeid(x) == typeid(double)) return true;... } return false;

Polymorphism A class which declares or inherits a virtual function is called a polymorphic class: class Av { }; public: virtual void foo(); class Bv : public Av { };...

Polymorphism Polymorphic classes call the intended virtual function despite variable type: class A {... void foo();...}; class B : public A {... void foo();...}; class Av {... virtual void foo();...}; class Bv : public Av {... virtual void foo();...}; A *aptr = new B; Av *avptr = new Bv; aptr->foo(); avptr->foo(); // non-polymorphic class // polymorphic class // calls A::foo() // calls Bv::foo()

typeid( ) respects polymorphism class A {... void foo();...}; class B : public A {... void foo();...}; class Av {... virtual void foo();...}; class Bv : public Av {... virtual void foo();...}; A *aptr = new B; Av *avptr = new Bv; // non-polymorphic class // polymorphic class cout << typeid(*aptr).name(); // prints A cout << typeid(*avptr).name();// prints Bv cout << typeid(aptr).name(); // prints A * cout << typeid(avptr).name(); // prints Av *

Polymorphic RTTI Example class ZCommunications {... }; class ZParallel : public ZCommunications {... }; class ZSCSI : public ZCommunications {... }; class ZFirewire : public ZCommunications {... }; void InitCommunications(ZCommunications &incomm) { if (typeid(incomm) == typeid(zparallel)) InitParallelCommunications(inComm); if (typeid(incomm) == typeid(zscsi)) InitSCSICommunications(inComm); } if (typeid(incomm) == typeid(zfirewire)) InitFirewireCommunications(inComm);

typeid( ) Tips You cannot determine the real type of an object pointed to by a void *. For non-polymorphic typed variables, typeid() gives info on the variable type. For polymorphic typed variables, typeid() gives info on the underlying real type. You cannot determine the name of an object s base class.

dynamic_cast< > dynamic_cast< > is used to cast one polymorphic type to another type within its inheritance chain dynamic_cast< > performs a safe down cast. dynamic_cast< > operations must be used on polymorphic pointers or references only.

dynamic_cast< > syntax dynamic_cast< > operation on polymorphic pointers and references: // pointer cast T *ptr1 = (T *) ptr2; T *ptr1 = dynamic_cast<t *>(ptr2); // reference cast T object1 = (T) object2; T object1 = dynamic_cast<t &>(object2); // compiler error T object1 = dynamic_cast<t>(object2);

dynamic_cast< > with pointers An incompatible pointer cast returns NULL: Av *aptr = new Av; Bv *bptr = new Bv; foo(aptr, bptr);... void foo(av *aptr1, Av *aptr2) { // bptr1 set to NULL Bv *bptr1 = dynamic_cast<bv *>(aptr1); } // bptr2 set to a valid Bv pointer Bv *bptr2 = dynamic_cast<bv *>(aptr2);

dynamic_cast< > with references An incompatible reference cast throws a bad_cast exception: Av Bv aobject; bobject; foo(aobject, bobject); void foo(av &aobject1, Av &aobject2) { // bobject1 throws a bad_cast exception Bv bobject1 = dynamic_cast<bv &>(aobject1); } // bobject2 set to a valid Bv object Bv bobject2 = dynamic_cast<bv &>(aobject2);

dynamic_cast< > Example class ZCommunications {... }; class ZParallel : public ZCommunications {... }; class ZSCSI : public ZCommunications {... }; class ZFirewire : public ZCommunications {... }; void InitCommunications(ZCommunications *inptr) { ZParallel *pptr = dynamic_cast<zparallel *>(inptr); if (pptr) InitParallelCommunications(inPtr); ZSCSI *sptr = dynamic_cast<zscsi *>(inptr); if (sptr) InitSCSICommunications(inPtr); } ZFirewire *fptr = dynamic_cast<zfirewire *>(inptr); if (fptr) InitFirewireCommunications(inPtr);

dynamic_cast< > vs. typeid( ) class ZCommunications {... }; class ZSCSI : public ZCommunications {... }; class ZSCSI_TSP : public ZSCSI {... }; void InitCommunications(ZCommunications &incomm) { // will not work if incomm is a ZSCSI_TSP } if (typeid(incomm) == typeid(zscsi)) InitSCSICommunications(inComm); void InitCommunications(ZCommunications *inptr) { // will work if incomm is a ZSCSI_TSP * } ZSCSI *sptr = dynamic_cast<zscsi *>(inptr); if (sptr) InitSCSICommunications(inPtr);

dynamic_cast< > Tips If you use dynamic_cast<> in the wrong place, you will get a compiler error. You cannot dynamic_cast<> a void *. You cannot dynamic_cast<> any nonpolymorphic type. If you have a non-polymorphic class heierarchy, use static_cast<>.

RTTI Gotcha s For typeid(), always #include <typeinfo.h>. The string returned in typeid(object).name() may differ slightly from compiler to compiler. Use dynamic_cast<> for polymorphic classes typeid() resolves polymorphic types for objects only, not pointers. dynamic_cast<>-ing works for pointers and references to polymorphic types, not object types Can t be used with Visual C++ v1.5.2 RTTI is defaulted off on Visual C++ 6

Demo