C++Learn C++

What Is The Right Way To Use reinterpret_cast In C++?

What Is The Right Way To Use reinterpret cast In C++

What is casting in C++? What is a cast operator? What types of casting can be used in a C++ app? What is reinterpret_cast in C++? How can I use reinterpret_cast in C++?

C++ is a fast and powerful programming language suitable for all types of purposes.

In this post, we will explain how to use reinterpret_cast in C++. Before this, let’s recap what is meant by ‘casting’ in C++.

What is casting in C++?

Casting is a technique to convert one data type to another data type. The operator used for this purpose is known as the cast operator. It is a unary operator which forces one data type to be converted into another data type.

In C++, a cast operator is an Unary Operator which forces one data type to be converted into another data type.
In general, C++ supports four types of casting:

  1. Static Cast (static_cast)
  2. Dynamic Cast (dynamic_cast)
  3. Constant Cast (const_cast)
  4. Reinterpret Cast (reinterpret_cast)

What is reinterpret_cast?

ReInterpret Cast (reinterpret_cast) is a cast operator that converts a pointer of some data type into a pointer of another data type, even if the the data types before and after conversion are different. reninterpret_cast does not check if the pointer type and data pointed by the pointer is same or not. In another terms reinterpret_cast converts type to another type by reinterpreting the underlying bit pattern which is pointed by a pointer.

reinterpret_cast only guarantees that if you cast a pointer to a different type, and then reinterpret_cast it back to the original type, you get the original value. So in the following:

Syntax for the reinterpret_cast:

Is there an example of how to use reinterpret_cast?

This is a simple example of how to use reinterpret_cast.

Here is a full example

This image has an empty alt attribute; its file name is CBuilder_11_512x512@1x.png

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, and Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Generic ProgrammingLearn C++Templates

What Is A Template In C++?

C++C++17Code SnippetComponentsIteratorsLearn C++

What Are The Boost C++ Libraries?

C++C++17Introduction to C++Learn C++Syntax

Learn Bits and How To Use std::bitset In C++

C++C++17IteratorsLearn C++

How To Use Insert In std::map In Modern C++