Why do we have reinterpret_cast in C++ when two chained static_cast can do its job?

后端 未结 7 1483
孤城傲影
孤城傲影 2020-12-01 01:58

Say I want to cast A* to char* and vice-versa, we have two choices (I mean, many of us think we\'ve two choices, because both seems to wor

7条回答
  •  失恋的感觉
    2020-12-01 02:33

    Look, people, you don't really need reinterpret_cast, static_cast, or even the other two C++ styles casts (dynamic* and const).

    Using a C style cast is both shorter and allows you to do everything the four C++-style cast let you do.

    anyType someVar = (anyOtherType)otherVar;
    

    So why use the C++-style casts? Readability. Secondly: because the more restrictive casts allow more code safety.

    *okay, you might need dynamic

提交回复
热议问题