CPP Virtuality and Casting Down
Virtuality and Casting Down Objects
- It is possible dynamic casting down objects from a base class to a derived class type at runtime, which can be done through using Run Time Type Identification (RTTI).
- To do this the class you downcast from has to be of polymorphic type, which is that at least one of the methods in the that class have to be virtual.
-
C++ have a function to test whether we can down cast an object or not:
CDerivedClass * driveClass = dynamic_cast<CDerivedClass*>(pObject);
- CDerivedClass is Class type that we want to down cast the object to.
- pObject is the variable that points to the object we want to down cast.
- driveClass results in NULL if we cannot down cast else we have got a right type down casted object and can use all methods, virtual or not, on that object.
Example:#include <iostream> class Person { public: Person (int age, int weight){ this->age = age; this->weight = weight; } // We want to use the RTTI so we // make Person class to be a polymorphic type // through defining the destructor to be virtual virtual ~Person () {} void showInfo() { std::cout << "I am a Person " << age << " years old " ; std::cout << "and weighs " << weight << " kilo.\n" ; } int getAge() const { return age; } int getWeight() const { return weight; } protected: int age; int weight; }; class Employee : public Person { public: Employee (int age, int weight, int salary): Person(age,weight){ this->salary = salary; } // Destructor is virtual as base class destructor is virtual ~Employee () {} void showInfo() { std::cout << "I am an Employee " << age << " years old " ; std::cout << "and weighs " << weight << " kilo " ; std::cout << "and earns " << salary << " dollar.\n" ; } int getSalary() const { return salary; } private: int salary; }; int main() { // Testing downcast using RTTI with // the base class, Person, which has a pointer to an Employee object Person * pObama = new Employee(45, 65, 50000); pObama->showInfo(); Employee * pEObama = dynamic_cast
if (pEObama!=NULL) { pEObama->showInfo(); }else { std::cout << "Cannot downcast the object\n" ; } std::cout << "\n" ; // Testing downcast using RTTI with // the base class, Person, which has a pointer to an Person object Person * pFrank = new Person(35, 70); pFrank->showInfo(); Employee * pEFrank = dynamic_cast if (pEFrank!=NULL) { pEFrank->showInfo(); }else { std::cout << "Cannot downcast the object\n" ; } std::cout << "\n" ; // Testing downcast using RTTI with // the base class, Person, which has a reference to an Employee object Employee Phil(42, 85, 70000); Person & rPhil=Phil; rPhil.showInfo(); Employee * pEPhil = dynamic_cast if (pEPhil!=NULL) { pEPhil->showInfo(); }else { std::cout << "Cannot downcast the object\n" ; } std::cout << "\n" ; return 0; } I am a Person 45 years old and weighs 65 kilo. I am an Employee 45 years old and weighs 65 kilo and earns 50000 dollar. I am a Person 35 years old and weighs 70 kilo. Cannot downcast the object I am a Person 42 years old and weighs 85 kilo. I am an Employee 42 years old and weighs 85 kilo and earns 70000 dollar.
You can download this example here (needed tools can be found in the right menu on this page).
© 2010 by Finnesand Data.
All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the
correctness in the pages or source codes.
The risk of using this web-site pages or any program
codes from this website is entirely at the individual user.