How to Use the static_cast Command in C++

Jinku Hu Feb 02, 2024
  1. Use static_cast to Explicitly Convert Types of Objects in C++
  2. Use reinterpret_cast to Explicitly Convert Types of Objects in C++
How to Use the static_cast Command in C++

This article will demonstrate multiple methods about how to use static_cast in C++.

Use static_cast to Explicitly Convert Types of Objects in C++

An operation that converts an object to a different type is called casting. There are cases when implicit conversions occur in C++ according to the language rules, e.g., array to pointer decay. Still, casting is mainly associated with the explicit conversion request that the user makes. When we cast a value of the object or the expression to a different type, we force the compiler to associate the given type to the pointer that points to the object.

There are four named explicit cast operations: const_cast, static_cast, reinterpret_cast, and dynamic_cast. These operations are native to modern C++ language and are relatively readable than the old C-style casts. The casts are often dangerous, and even experienced programmers make mistakes with them, but you should not be discouraged from utilizing these conversion operations when necessary. In this article, we only overview the static_cast and reinterpret_cast operations.

The static_cast function is generally used to convert related types as pointers of the same class hierarchy or numeric types to each other. This command also handles conversions defined by constructors and conversion operators. Note that the second line in the main function is essentially performing the implicit cast from the signed char to a signed integer, which is just a bit of the obscured version of the next line.

It’s the recommended way to conduct the casting in contemporary C++, even though the result is the same. On the other hand, the fourth and fifth lines of the main function are not valid conversions using the static_cast operation. Although, we can force the latter conversion using the C-style cast, (int*)x, which prints the 97 integer value with hexadecimal form and memory address notation. Note that this operation will most likely generate a compiler warning.

#include <iostream>

using std::cout;
using std::endl;

int main() {
  char x = 'a';
  int x_i = x;
  int x_ii = static_cast<int>(x);
  int* x_iii = static_cast<int*>(x);   // ERROR
  int* x_iii = static_cast<int*>(&x);  // ERROR
  int* x_iii = (int*)x;                // WARNING

  cout << x << endl;
  cout << x_i << endl;
  cout << x_ii << endl;
  cout << x_iii << endl;

  return EXIT_SUCCESS;
}

Output:

a
97
97
0x61
0x7ffeb7c31997

Use reinterpret_cast to Explicitly Convert Types of Objects in C++

Alternatively, the latter C-style cast can be done using the reinterpret_cast operation shown in the following code example. This method will silence the compiler warning, and the user can take responsibility for the conversion. We can convert different pointer types using the reinterpret_cast like char* to int*.

In this case, the printed address is the same as the one where the x character is stored. Remember that if we dereference the x_iii pointer to access the value, we won’t get the character a or its ASCII alternative, but rather a strange integer. This integer is retrieved from the same location. Only the size of the data type is different since it’s interpreted as the int.

#include <iostream>

using std::cout;
using std::endl;

int main() {
  char x = 'a';
  int* x_i = (int*)x;  // WARNING
  int* x_ii = reinterpret_cast<int*>(x);
  int* x_iii = reinterpret_cast<int*>(&x);

  cout << x_i << endl;
  cout << x_ii << endl;
  cout << x_iii << endl;

  return EXIT_SUCCESS;
}

Output:

0x61
0x61
0x7ffca18be95f
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ Cast