static_cast


Keyword: static_cast

static_cast is one of the four casting operators in C++. It's used for explicit type conversions that are considered safe by the compiler. This cast is performed at compile-time and is the most commonly used casting operator in C++.

Key Characteristics

Example 1: Basic Usage: Numeric Type Conversion

#include <iostream>

int main() {
    double pi = 3.14159;
    int intPi = static_cast<int>(pi);

    std::cout << "Original pi: " << pi << std::endl;
    std::cout << "Integer pi: " << intPi << std::endl;

    return 0;
}

Explanation:

Example 2: Pointer Conversion in Inheritance Hierarchy

#include <iostream>

class Base {
public:
    virtual void print() { std::cout << "Base" << std::endl; }
    virtual ~Base() {}
};

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

int main() {
    Base* basePtr = new Derived();
    basePtr->print();  // Prints "Derived"

    // Downcasting
    Derived* derivedPtr = static_cast<Derived*>(basePtr);
    derivedPtr->print();  // Prints "Derived"

    delete basePtr;
    return 0;
}

Explanation:

Example 3: Converting void* to Typed Pointer

#include <iostream>
#include <cstdlib>

int main() {
    // Allocate memory
    void* memory = std::malloc(sizeof(int));

    // Convert void* to int*
    int* intPtr = static_cast<int*>(memory);

    // Use the memory
    *intPtr = 42;

    std::cout << "Value: " << *intPtr << std::endl;

    // Free the memory
    std::free(memory);

    return 0;
}

Explanation:

Example 4: Explicit Constructor Call

#include <iostream>
#include <string>

class Person {
public:
    explicit Person(const std::string& name) : name_(name) {}
    void introduce() const { std::cout << "My name is " << name_ << std::endl; }

private:
    std::string name_;
};

int main() {
    std::string name = "Alice";

    // Using static_cast to call explicit constructor
    Person person = static_cast<Person>(name);
    person.introduce();

    return 0;
}

Explanation:

Additional Considerations

  1. Safety: While safer than C-style casts, static_cast still requires caution, especially with pointer conversions.

  2. Performance: static_cast has no runtime cost as it's resolved at compile-time.

  3. Alternatives: For downcasting, consider dynamic_cast if runtime type checking is needed.

  4. Readability: Using static_cast makes the intention of type conversion explicit in the code.

Summary

static_cast is a versatile and type-safe casting operator in C++. It's used for explicit type conversions between related types, including numeric conversions, pointer conversions in class hierarchies, and explicit constructor calls. Unlike C-style casts, static_cast provides compile-time checking, making it safer and more expressive. However, it doesn't perform runtime checks, so it's crucial to use it correctly, especially when dealing with pointers in inheritance hierarchies. static_cast is preferred over C-style casts in modern C++ due to its improved type safety and clarity of intent. While powerful, it should be used judiciously, and alternatives like dynamic_cast should be considered when runtime type checking is necessary.

Previous Page | Course Schedule | Course Content