C++ static_cast
In this page:
target_type variable_name = static_cast<target_type>(expression);
What is static_cast?
The static_cast operator is used for standard, compile-time type conversions. It converts one data type to another, such as float to integer, and is checked by the compiler to ensure the conversion is safe.
Example: What is static_cast?
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare f and set it to 3.9f
float f = 3.9f;
// Declare i and set it to static_cast<int>(f)
int i = static_cast<int>(f);
// Print to the console with cout
std::cout << i << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Enum Conversions
You can use static_cast to convert scoped enums (enum classes) to their underlying integer values, or to convert integers back to enum values.
Example: Enum Conversions
// Include std::cout and std::cin
#include <iostream>
enum class Color { Red, Green, Blue };
// Program execution starts in main()
int main() {
// Declare value and set it to static_cast<int>(Color::Green)
int value = static_cast<int>(Color::Green);
// Print to the console with cout
std::cout << value << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Upcasting in Inheritance
You can use static_cast to cast a derived class pointer up to a base class pointer. This type of conversion is always safe because a derived object contains all the components of its base class.
Example: Upcasting in Inheritance
// Include std::cout and std::cin
#include <iostream>
// Define the type Animal
class Animal {};
// Define the type Dog
class Dog : public Animal {};
// Program execution starts in main()
int main() {
Dog d;
Animal *a = static_cast<Animal *>(&d);
// Print to the console with cout
std::cout << "Upcast succeeded" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Void Pointer Conversions
A void pointer (void*) is a generic pointer that has no associated data type. You can use static_cast to convert a void pointer back to its original typed pointer so you can dereference it.
Example: Void Pointer Conversions
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare value and set it to 42
int value = 42;
void *vptr = &value;
int *iptr = static_cast<int *>(vptr);
// Print to the console with cout
std::cout << *iptr << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
static_cast vs C-style Casts
Unlike older C-style casts like (int)value, static_cast is safer because it is checked by the compiler. It prevents you from accidentally making invalid and unsafe type conversions.
Example: static_cast vs C-style Casts
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare d and set it to 3.9
double d = 3.9;
// Declare a and set it to (int)d
int a = (int)d;
int b = static_cast<int>(d);
// Print to the console with cout
std::cout << a << " " << b << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using
static_castbetween unrelated types, which does not compile. - Assuming
static_castchecks safety at runtime, when it does not. - Downcasting to the wrong derived type, which is undefined behavior.
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced