Explicit Casting in C++

Amit.Kumar
2 min readJun 9, 2023

static_cast:

  • Definition: Performs conversions between related types, such as numeric types, pointers, and references, that are known at compile-time.
  • Usage: Used for casting numeric types to other numeric types, upcasting and downcasting in inheritance hierarchies, and converting pointers/references within a class hierarchy.
// Casting numeric types
int num = 10;
double doubleNum = static_cast<double>(num);

// Upcasting and downcasting in inheritance hierarchy
class Base {
// Base class definition
};
class Derived : public Base {
// Derived class definition
};

Base* basePtr = new Derived();
Derived* derivedPtr = static_cast<Derived*>(basePtr);

dynamic_cast:

  • Definition: Performs a runtime type check to ensure the validity of a polymorphic type conversion.
  • Usage: Primarily used for casting pointers/references between base and derived classes in polymorphic scenarios. It checks if the conversion is valid and returns a null pointer if it fails.
// Casting between base and derived classes in polymorphic scenario
class Base {
virtual void print() {}
};
class Derived : public Base {
void print() override {}
};

Base* basePtr = new Derived();
Derived* derivedPtr = dynamic_cast<Derived*>(basePtr);

if (derivedPtr != nullptr) {
// Dynamic cast successful
derivedPtr->print();
}

const_cast:

  • Definition: Removes const or volatile qualifiers from variables, allowing modification.
  • Usage: Used to cast away the const or volatile qualifier from a variable, enabling modifications. It should be used with caution, as modifying a const object results in undefined behavior.
// Removing const qualifier from variable
const int num = 10;
int* mutablePtr = const_cast<int*>(&num);
*mutablePtr = 20; // Modifying const variable through const_cast (leads to undefined behavior)

reinterpret_cast:

  • Definition: Converts a pointer to any other pointer type, even unrelated types.
  • Usage: Typically used when converting between unrelated pointer types, such as casting a pointer to an integer type or casting a void pointer to a specific type. It should be used sparingly, as it can easily lead to type safety issues.
// Converting pointer to an unrelated pointer type
int num = 10;
void* voidPtr = reinterpret_cast<void*>(&num);
int* intPtr = reinterpret_cast<int*>(voidPtr);

static_cast (again):

  • Definition: Performs implicit conversions between implicitly convertible types, including numeric types and pointers.
  • Usage: Used for implicit conversions between numeric types, converting pointers to void pointers or between unrelated pointer types, and other safe conversions.
// Implicit conversion between numeric types
double doubleNum = 3.14;
int intNum = static_cast<int>(doubleNum);

// Converting pointer to void pointer or between unrelated pointer types
int num = 10;
void* voidPtr = static_cast<void*>(&num);
int* intPtr = static_cast<int*>(voidPtr);

--

--

Amit.Kumar

I have been a coder all my life . And yes a dreamer too. But i am also interested in understanding different aspects of life .