-
October 10th, 2013, 03:08 PM
#1
[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();
}
-
October 10th, 2013, 03:43 PM
#2
Re: What is the difference between static_cast and dynamic_cast?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 10th, 2013, 03:49 PM
#3
Re: What is the difference between static_cast and dynamic_cast?
-
October 11th, 2013, 04:23 PM
#4
Re: [RESOLVED] What is the difference between static_cast and dynamic_cast?
![Quote](images/misc/quote_icon.png) 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.
-
October 12th, 2013, 11:14 AM
#5
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.
-
October 15th, 2013, 02:08 AM
#6
Re: [RESOLVED] What is the difference between static_cast and dynamic_cast?
![Quote](images/misc/quote_icon.png) 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.
Last edited by razzle; October 15th, 2013 at 02:13 AM.
-
October 15th, 2013, 08:48 AM
#7
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.
-
October 15th, 2013, 10:37 AM
#8
Re: [RESOLVED] What is the difference between static_cast and dynamic_cast?
![Quote](images/misc/quote_icon.png) 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#).
Last edited by razzle; October 15th, 2013 at 10:44 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|