C++ static_cast Keyword

The static_cast keyword in C++ is used to perform type casting at compile-time. It is a safer and more explicit way of converting one type to another compared to C-style casting. The static_cast is used for standard type conversions like converting numeric types, casting pointers, or converting between related class types.

The static_cast does not perform runtime checks and should only be used when the conversion is well-defined and safe. Improper use may lead to undefined behavior.


Syntax

</>
Copy
static_cast<new_type>(expression);
new_type
The target type to which the expression is cast.
expression
The value or object being cast to the new type.

Examples

Example 1: Converting Between Numeric Types

This example demonstrates using static_cast to convert between numeric types.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    double num = 3.14;
    int integerPart = static_cast<int>(num); // Convert double to int
    cout << "Original number: " << num << endl;
    cout << "Integer part: " << integerPart << endl;
    return 0;
}

Output:

Original number: 3.14
Integer part: 3

Explanation:

  1. The static_cast<int> converts the double num to an integer, truncating the fractional part.
  2. This type conversion is explicitly stated, making the code safer and more readable than C-style casting.

Example 2: Casting Pointers

This example shows how static_cast can be used to cast pointers between related types.

</>
Copy
#include <iostream>
using namespace std;

class Base {
public:
    virtual void show() { cout << "Base class" << endl; }
};

class Derived : public Base {
public:
    void show() override { cout << "Derived class" << endl; }
};

int main() {
    Derived d;
    Base* basePtr = static_cast<Base*>(&d); // Cast Derived* to Base*
    basePtr->show();
    return 0;
}

Output:

Derived class

Explanation:

  1. The pointer basePtr is cast from Derived* to Base* using static_cast.
  2. The cast is safe because Derived is derived from Base.
  3. The virtual function show() ensures that the correct function in the derived class is called.

Example 3: Preventing Invalid Casts

In this example, static_cast is used to avoid unintended casts that may lead to undefined behavior.

</>
Copy
#include <iostream>
using namespace std;

void printValue(void* ptr) {
    int* intPtr = static_cast<int*>(ptr); // Explicit cast from void* to int*
    cout << "Value: " << *intPtr << endl;
}

int main() {
    int num = 42;
    void* voidPtr = &num; // Implicit cast to void*
    printValue(voidPtr);
    return 0;
}

Output:

Value: 42

Explanation:

  1. The function printValue casts the void* back to an int* using static_cast.
  2. Using static_cast makes the type conversion explicit and avoids unsafe implicit casts.
  3. The program correctly dereferences the pointer to print the value of the integer.

Key Points to Remember about static_cast

  1. static_cast performs compile-time type checking, making it safer than C-style casting.
  2. It is used for standard type conversions, such as numeric conversions or casting between related types.
  3. It cannot perform runtime checks; use dynamic_cast for downcasting with runtime type checking.
  4. Improper use of static_cast may lead to undefined behavior, so it should be used only when the conversion is safe and well-defined.
  5. It is more explicit and readable than C-style casting, improving code clarity.