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

后端 未结 7 1492
孤城傲影
孤城傲影 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:24

    Other than the practical reasons that others have given where there is a difference in what they can do it's a good thing to have because its doing a different job.

    static_cast is saying please convert data of type X to Y. reinterpret_cast is saying please interpret the data in X as a Y.

    It may well be that the underlying operations are the same, and that either would work in many cases. But there is a conceptual difference between saying please convert X into a Y, and saying "yes I know this data is declared as a X but please use it as if it was really a Y".

提交回复
热议问题