static_cast vs const_cast vs dynamic_cast vs reinterpret_cast

static_cast
  1. used for general type conversions that well-defined and safe.
  2. performs compile time type checking but does not perform runtime type checking.
  3. can be used for implicit conversions, numeric conversions, upcasting, downcasting(if the relationship is known at compile time).

    enum class E { ONE = 1, TWO, THREE };
    // scoped enum to int
    E e = E::TWO;
    int two = static_cast<int>(e);

const_cast
  1. used to add or remove the const or volatile qualifiers from a variable.
  2. can be used to modify non-const variables through a pointer or reference to const

    int i = 3;                 // i is not declared const
    const int& rci = i;
    const_cast<int&>(rci) = 4; // OK: modifies i

dynamic_cast
  1. used for safe downcasting and runtime type checking in the context of polymorphic classes
  2. perform runtime type checking and returns a null pointer if the conversion is not possible(when casting pointers) or throws a std::bad_cast exception(when casting reference).
  3. checks if the casted object s type is compatible with the target type.

    struct V {};
    struct A : virtual V{};
    struct B : virtual V{};
    struct D : A, B {};
    D d; // the most derived object
    A& a = d; // upcast, dynamic_cast may be used, but unnecessary
    D& new_d = dynamic_cast<D&>(a); // downcast
    B& new_b = dynamic_cast<B&>(a); // sidecast

reinterpret_cast
  1. used for low-level, unsafe conversions between unrelated types.
  2. No type checking is performed, and result may not be meaningful or well-defined.
  3. use with extrem caution, it can easily lead to undefined behavior.

    int i = 7;
    // type aliasing through pointer
    char* p2 = reinterpret_cast<char*>(&i);

© 版权声明

相关文章

暂无评论

none
暂无评论...