![]() ![]() A base class has, therefore, many forms: the base class itself, and any of its subclasses. In addition to allowing you to use dynamic cast, it also allows the proper destructors to be called when a base class pointer is deleted. Polymorphism, which etymologically means 'many forms,' is the ability to treat an object of any subclass of a base class as if it were an object of the base class. The simplest case, and probably worthwhile if you're passing pointers around like this, is to consider making the base class's destructor virtual. However, if the type in question does not have any virtual methods, then it cannot be used. ![]() It requires that the class in question have at least 1 virtual method, which allows the compiler (if it supports RTTI) to perform this additional check. A polymorphic variable is a variable that can hold values of different types. But the check is only at compile time, and the compiler assumes you know what you are doing.ĭynamic_cast can only be used in the case of a pointer or reference cast, and in addition to the compile time check, it does an additional run time check that the cast is legal. A simple and easy program to understand the concept of runtime polymorphism, compile time polymorpism,overloading, overridding, downcasting, upcasting and Interface. Self and Super Downcasting (Reverse Polymorphism) Pure Polymorphism. C++ allows that a derived class pointer (or reference) to be treated as a base class pointer. The reason why: static_cast basically has the compiler perform a check at compile time "Could the input be cast to the output?" This is can be used for cases where you are casting up or down an inheritance hierarchy of pointers (or references). It can be achieved by using Polymorphism. static_cast is the cast you would use in this case, if you know that it is in fact an object of the target type. Im reading up a book on Java and currently on the Polymorphism topic as well as how to downcast a reference variable. Downcasting and run-time type identification. Syntax errors non-withstanding, you cannot dynamic_cast a non-polymorphic type. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |