View Single Post
  #4 (permalink)  
Old March 23rd, 2005, 01:28 PM
jacob jacob is offline
Friend of Wrox
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts

No, not nessesarily :)

If you have several classes which are derived from the same parent class, you might not know what type of class you are dealing with. Say you made a method which as input would take some kind of animal class and this class was used to make child classes such as Dog, Cat etc., then it would be nice to dynamically cast the objects that was given to the method (Dog, Cat etc).

In C++ this is done using dynamic_cast, and if it returns null it is not of the type you are trying to cast it to. An example from C++ in this case would be...
BlockA* ba = dynamic_cast<BlockA*>(eblock);
if(ba != NULL)
The problem I had was a bit different since I wanted to give the type, into which I would cast, as an argument to some method.