Why can't I static_cast between char * and unsigned char *?

前端 未结 3 2042
清歌不尽
清歌不尽 2020-12-05 04:03

Apparently the compiler considers them to be unrelated types and hence reinterpret_cast is required. Why is this the rule?

3条回答
  •  -上瘾入骨i
    2020-12-05 04:33

    Aside from being pointers, unsigned char * and char * have nothing in common (EdChum already mentioned the fact that char, signed char and unsigned char are three different types). You could say the same thing for Foo * and Bar * pointer types to any dissimilar structures.

    static_cast means that a pointer of the source type can be used as a pointer of the destination type, which requires a subtype relationship. Hence it cannot be used in the context of your question; what you need is either reinterpret_cast which does exactly what you want or a C-style cast.

提交回复
热议问题