CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Casting references VS Casting the mem address of that reference?

  1. #1
    Join Date
    Jul 2017
    Location
    Greece
    Posts
    127

    Question Casting references VS Casting the mem address of that reference?

    If you want to cast a reference you usually do this:

    Code:
    Subclass &myref = (Subclass&)base_class_object_ref;
    But I have also seen people doing this:
    Code:
    Subclass &myref = *(Subclass*)&base_class_object_ref;
    I understand how it works, but is there any difference? Is there a reason or a specific scenario where I must choose the one over the other?

    Thanks

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

    Re: Casting references VS Casting the mem address of that reference?

    Neither should be used. In C++ you would use a dynamic_cast for a downcast - not a 'c' style cast.

    Code:
    Subclass &myref = dynamic_cast<Subclass&>(base_class_object_ref);
    For an upcast, you don't need to cast.

    See https://en.cppreference.com/w/cpp/language/dynamic_cast
    Last edited by 2kaud; September 6th, 2020 at 07:53 AM.
    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++17 Compiler: Microsoft VS2019 (16.7.7)

  3. #3
    Join Date
    Jul 2017
    Location
    Greece
    Posts
    127

    Re: Casting references VS Casting the mem address of that reference?

    LOL, I thought dynamic cast would fail on a downcast and work only for upcasts. Is it the other way around?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,374

    Re: Casting references VS Casting the mem address of that reference?

    Quote Originally Posted by babaliaris View Post
    LOL, I thought dynamic cast would fail on a downcast and work only for upcasts. Is it the other way around?
    Why not try it?

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,289

    Re: Casting references VS Casting the mem address of that reference?

    Quote Originally Posted by babaliaris View Post
    LOL, I thought dynamic cast would fail on a downcast and work only for upcasts. Is it the other way around?
    read the documentation at the given link.
    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++17 Compiler: Microsoft VS2019 (16.7.7)

  6. #6
    Join Date
    Feb 2017
    Posts
    572

    Re: Casting references VS Casting the mem address of that reference?

    Quote Originally Posted by babaliaris View Post
    LOL, I thought dynamic cast would fail on a downcast and work only for upcasts. Is it the other way around?
    Assigning a derived type to a base type is typesafe meaning the correctness can be checked at compile-time already. Because of that, explicit upcasting is never necessary (although allowed).

    An assignment the other way around, from base to derived, is not typesafe and may fail at runtime. To make this perfectly clear the compiler requires the programmer to put in an explicit downcast, either static or dynamic. For a dynamic downcast the compiler inserts a runtime check which means it will fail in a way the programmer can detect and act on, whereas a static downcast will fail in silence.

    Since downcasting isn't typesafe the general recommendation is to avoid it. In object oriented (OO) programming, if there are many downcasts this is usually a sign of a design flaw. It could for example be that the functionality exposed by the base type is too limited and so forces the programmer to reach for the derived types. But sometimes downcasting is motivated and then OO offers a typesafe way called the Visitor design pattern.

    One of the major benefits of C++ is its strong type checking at compile-time so it's kind of counterproductive to prevent the compiler from doing its job properly by putting in downcasts. And even if a dynamic downcast will catch an assignment error at runtime it comes with a check that adds overhead.
    Last edited by wolle; September 10th, 2020 at 03:06 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)