Click here to Skip to main content
15,904,287 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

what is dynamic_cast? and where can i use dynamic_cast?

Please describe?

Regards,
Ranjith
Posted
Comments
Philippe Mori 25-Dec-12 9:25am    
For question like that, you can have an answer in less than a second using Google.

First result is dynamic_cast.

First lets define a few concepts: class hierarchy, upcast, downcast.
A class hierarchy in UML is often visualized as a class tree (sometimes graph) the way this picture illustrates: http://www.ccs.neu.edu/research/demeter/personalities/luis/example/Image1.gif[^]
As you see the root of the tree is the base class and it is at the top of the picture. If you have a pointer to a derived class instance and you cast the pointer into a pointer of one of its base classes then you are doing an "upcast" because you cast from one of the lower nodes of the hierarchy tree to another node that is higher in the tree. This type of cast is safe and straightforward so you don't even have to indicate this cast for the compiler, it will do the cast automatically:
C++
class Base {};
class Derived : public Base {};
Derived* d = blahblah;
Base* b = d;    // automatic upcast

However when you cast from a base class to a derived class it is called a downcast that is considered unsafe. The compiler doesn't know whether a base class pointer of type "Animal" points to a Tiger instance or a Mouse instance... But if the base class has a vtable (because it has at least 1 virtual method) then you can use dynamic_cast to ask the compiler to generate some code that finds out whether your base class pointer points to a particular subclass instance or not:
C++
class Base
{
    // making sure that this class hierarchy has a vtable
    // because the vtable contains the typeinfo needed by dynamic_cast
    virtual ~Base() {}
};
class Derived1 : public Base {};
class Derived2 : public Base {};
Base* b = new Derived1;
Derived1* d1 = dynamic_cast<derived1*>(b);  // d1 will be non-NULL
Derived2* d2 = dynamic_cast<derived2*>(b);  // d2 will be NULL

dynamic_cast uses the typeinfo found in the vtable (usually at a negative offset below the method pointers) thats why your base class has to have at least one virtual function.

Final notes: using dynamic_cast (and downcast in general in programming in any object oriented language) defeats the purpose of polimorphism. Its like using if-then-else instead of true object oriented design. Using downcasts in an object oriented program (dynamic_cast in C++) is a strong indicator of bad code/OO design. Another drawback is that dynamic_cast can be relatively slow compared to other kind of safe casts.
If your code has dynamic_casts in it then your OO design sucks! I allow using dynamic_cast for myself at most once a year only in case of hotfixes when a little dirty hack is excepted instead of a redesign and the code is not performance critical.
 
Share this answer
 
v3
Comments
ranjithkumar81 26-Dec-12 0:25am    
Yes, I understood your description and where can i use dymanic_cast?, and why i need to do dynamic_cast?

please explain.
pasztorpisti 26-Dec-12 9:15am    
The answer is at the end of my post. You SHOULDN'T use dynamic_cast, or more accurately: you MUSTN'T. It's a hack in the object oriented programs.
dynamic_cast is almost exclusively used for handling polymorphism. You can cast a pointer or reference to any polymorphic type to any other class type (a polymorphic type has at least one virtual function, declared or inherited). You don't have to use it to cast downwards, you can cast sideways or even up another chain. The dynamic_cast will seek out the desired object and return it if possible. If it can't, it will return NULL in the case of a pointer, or throw std::bad_cast in the case of a reference.

dynamic_cast has some limitations, though. It doesn't work if there are multiple objects of the same type in the inheritance hierarchy (the so-called 'dreaded diamond') and you aren't using virtual inheritance. It also can only go through public inheritance - it will always fail to travel through protected or private inheritance. This is rarely an issue, however, as such forms of inheritance are rare.

Reference : stackoverflow.com[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900