C++ Succinctly - Casting in C++
C++ Succinctly - Casting in C++
Overview
There are ve different ways of casting variables. There is overlap between them,
especially with the C-style cast and all other casts, but each has its use. It is good to learn
them all, so you can use the best cast for your particular need rather than using any cast
that happens to work. If you ever need a quick reference, I recommend this post on
StackOver ow.
I’m not discussing implicit casting here for the simple reason that it’s a basic concept with
an almost in nite number of variations. If I write float f = 10; I’ve implicitly cast an
https://code.tutsplus.com/articles/c-succinctly-casting-in-c--mobile-22058 1/7
12/9/2020 C++ Succinctly: Casting in C++
p y
integer to a oat and stored its result in f. You can also implicitly cast an object of type B
to a pointer to its base class A using the address-of operator or to a reference to its base
class A by doing a normal assignment.
const_cast
The const_cast operator can add and remove const and volatile. Using it to add either of
these attributes is ne. It’s rare that you would, but if you do, you can.
Its ability to remove const is something you should never use in a C++ program except
when you need to call a C-language function that doesn’t abide by const-correctness but
does not modify the object at all. If a function has a const parameter, and throws out the
const-ness of it by using const_cast, the function is breaking the implied contract that it
will not modify the parameter. So it’s up to you as the author of that function to ensure that
you are not going to modify the object; otherwise, you should not use const for the
parameter since you will be modifying the object.
If you ever need to use const_cast and another cast operator on the same object, use
const_cast last, since removing const-ness from an object could allow unintended
changes to take place if you used a subsequent cast.
Advertisement
static_cast
static_cast. Typically, static_cast should be your rst choice of casts, as it does all the
checking it can do at compile-time, so you don’t have added run-time checking to slow
down your program.
dynamic_cast
The dynamic_cast operator is useful for casting through virtual inheritance. static_cast
can cast from a derived class to a base class, whether the inheritance is virtual or not. Say,
however, you are given an object of type A, but you know it is actually an object of type B—
and that B inherits virtually from A. If you want to cast this object back to B to use member
functions that only B provides, you need to use dynamic_cast.
reinterpret_cast
The reinterpret_cast operator is a direct conversion with very few good uses. Most its
operations give unde ned results. What this means in practice is that you should read the
compiler vendor’s documentation before using it for anything.
One use for it, as we saw in StorageDurationSample , is to cast a pointer to an integer type
large enough to hold it. This gives the memory address of the pointer, which can be useful
for debugging and tracing operations where you can dump data to log les and create
core dumps, but it may not be able to run a debugger easily. You will see it used
legitimately at times for other purposes, but in general, it should be considered as the cast
of last resort (excluding a C-style cast, which comes after reinterpret_cast).
Advertisement
https://code.tutsplus.com/articles/c-succinctly-casting-in-c--mobile-22058 3/7
12/9/2020 C++ Succinctly: Casting in C++
C-style Cast
The C-style cast, (e.g., auto someData = (SomeType)dataOfSomeOtherType; ) is not your friend.
You are undoubtedly familiar with it from C#, where it is highly useful. In C#, if you try to
make a cast using that syntax, and the cast is invalid, you will produce an
InvalidCastException. This happens because the CLR keeps track of the types of
everything you’ve created and detects bad casts.
C++ does not check to see if your C-style cast is valid, assuming it compiles, of course.
C++ just assumes that it is. If it’s a bad cast, and you are lucky, your program will crash
immediately. If not, you will end up with data in an unknown state, which will certainly
become corrupted in subtle and insidious ways.
Further, unlike the other casts, which you can easily spot by looking for _cast<, C-style
casts do not stick out. When you’re scanning lots of code quickly, parentheses wrapped
around text looks as much like a function call as it does a cast operation. You could use a
regular expression search for this in Visual Studio 2012: \(.*\)[A-Za-z]. Even so, you are still
forgoing all the bene ts and protections of the other casts.
The one thing a C-style cast can do that other casts cannot is cast an object to one of its
protected or private inheritance base classes. You really shouldn’t do this since, if you
need public inheritance, you should use public inheritance.
Sample
There is a sample, CastingSample, that demonstrates the many possible types of casting.
It is included with the source code for this series. In the interest of brevity, I am omitting it
here.
Conclusion
https://code.tutsplus.com/articles/c-succinctly-casting-in-c--mobile-22058 4/7
Conclusion
12/9/2020 C++ Succinctly: Casting in C++
In this article, we've covered casting in C++ and I hope it's clear that you shouldn't use C-
style casts. The next article zooms in on strings in C++.
Advertisement
Michael
McLaughlin
Michael McLaughlin
Michael is the founder and lead developer at Bob Taco Industries. He primarily
develops games and mobile applications for Windows and Windows Phone.
Email Address
Update me weekly
Download Attachment
Translations
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!
Powered by
Native
Advertisement
https://code.tutsplus.com/articles/c-succinctly-casting-in-c--mobile-22058 6/7
12/9/2020 C++ Succinctly: Casting in C++
ENVATO TUTS+
HELP
envato-tuts+
© 2020 Envato Pty Ltd. Trademarks and brands are the property of their respective owners.
https://code.tutsplus.com/articles/c-succinctly-casting-in-c--mobile-22058 7/7