CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    [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();
        }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  3. #3
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    Re: What is the difference between static_cast and dynamic_cast?

    Right, thanks.

  4. #4
    Join Date
    Jul 2013
    Posts
    576

    Re: [RESOLVED] What is the difference between static_cast and dynamic_cast?

    Quote Originally Posted by vincegata View Post
    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.

  5. #5
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    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.

  6. #6
    Join Date
    Jul 2013
    Posts
    576

    Re: [RESOLVED] What is the difference between static_cast and dynamic_cast?

    Quote Originally Posted by vincegata View Post
    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.

  7. #7
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    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.

  8. #8
    Join Date
    Jul 2013
    Posts
    576

    Re: [RESOLVED] What is the difference between static_cast and dynamic_cast?

    Quote Originally Posted by vincegata View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured