-
September 6th, 2020, 02:42 AM
#1
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
-
September 6th, 2020, 07:03 AM
#2
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
September 7th, 2020, 12:55 AM
#3
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?
-
September 7th, 2020, 02:03 AM
#4
Re: Casting references VS Casting the mem address of that reference?
Originally Posted by babaliaris
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?
-
September 7th, 2020, 02:09 AM
#5
Re: Casting references VS Casting the mem address of that reference?
Originally Posted by babaliaris
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
September 7th, 2020, 03:02 AM
#6
Re: Casting references VS Casting the mem address of that reference?
Originally Posted by babaliaris
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|