c++castingstatic-cast

What is the difference between static_cast<> and C style casting?


Is there any reason to prefer static_cast<> over C style casting? Are they equivalent? Is there any sort of speed difference?


Solution

  • C++ style casts are checked by the compiler. C style casts aren't and can fail at runtime.

    Also, C++ style casts can be searched for easily, whereas it's really hard to search for C style casts.

    Another big benefit is that the 4 different C++ style casts express the intent of the programmer more clearly.

    When writing C++ I'd pretty much always use the C++ ones over the the C style.