[RESOLVED] What is the difference between static_cast and dynamic_cast?
Hi
Is there a difference between having static and dynamic cast in this scenario? The output is the same.
THX
Code:
Base* pb = new Derived();
if(Derived* pd2 = static_cast<Derived*>(pb)) // true
{
pd2->get_price(); // calls Base::get_price()
pd2->get_rate(); // calls Derived::get_rate()
pd2->calc(); // calls Derived::calc() (it overrides virtual Base::calc())
}
if(Derived* pd = dynamic_cast<Derived*>(pb)) // true
{
pd->get_price();
pd->get_rate();
pd->calc();
}
Re: What is the difference between static_cast and dynamic_cast?
Re: What is the difference between static_cast and dynamic_cast?
Re: [RESOLVED] What is the difference between static_cast and dynamic_cast?
Quote:
Originally Posted by
vincegata
Is there a difference between having static and dynamic cast in this scenario? The output is the same.
If you're using the object oriented programming paradigm and writing polymorphic code in C++ you should never downcast. That is never do a narrowing conversion (from Base to Derived). Neither with static_cast nor with dynamic_cast.
A widening conversion (from Derived to Base) on the other hand is fine and what polymorphism is all about really. It doesn't even require explicit casting.
Re: [RESOLVED] What is the difference between static_cast and dynamic_cast?
That's true when the program is all well designed but for other cases Stroustrup came up with casting. downcasting is used when you cannot have a virtual function, e.g. you do not have control over the base class, so you have a base class type pointer pointing to the derived object but you want to call a (non-virtual) function from the derived class so you donwcast base pointer to derived pointer to do that.
Re: [RESOLVED] What is the difference between static_cast and dynamic_cast?
Quote:
Originally Posted by
vincegata
Stroustrup came up with casting
He also advices against its usage. In The C++ Programming Language, fourth edition, under 11.6 Advice:
"[12] Avoid explicit type conversion (casts)"
So if you find yourself in a position where your OO design forces you to downcast then change the design. Don't spoil the integrity of your code with not typesafe constructs like downcasting.
Re: [RESOLVED] What is the difference between static_cast and dynamic_cast?
Sure. As I mentioned above downcasting is for the cases when you do not have control over the base classes.
Re: [RESOLVED] What is the difference between static_cast and dynamic_cast?
Quote:
Originally Posted by
vincegata
Sure. As I mentioned above downcasting is for the cases when you do not have control over the base classes.
Rather than allowing a crappy design to infect new code I would introduce a new design using the existing classes to implement it. In a sense this is the Adapter design pattern. Usually this removes the need for downcasting since the old classes are no longer used polymorphically, only as concrete implementation objects.
I've never encountered a situation where downcasting couldn't be avoided resulting in much healthier code. But fair enougth, if you really have no option then at least use a dynamic_cast. Then the typechecking you skipped at compile time will be performed at runtime. This is the same level of security you get if you downcast in Java (and I guess C#).