uint8_t* buf1;
...
const signed char* buf2 = static_cast<const signed char*>(buf1);
invalid static_cast from type 'uint8_t* {aka unsigned char*}' to type 'const signed char*'
c-style-cast: (const signed char*) 工作正常
在这种情况下使用 c-style-cast 与 static_cast 有什么危险吗?
最佳答案
Is there any danger using c-style-cast vs static_cast in this case?
static_cast 在这种情况下根本不是一个选项,正如错误消息所解释的那样。
使用 c-style cast 的危险在于您可能没有打算执行 reinterpret_cast,而 c-style cast 在这里执行。如果您打算执行 reinterpret_cast,请使用 reinterpret_cast。如果你打算使用 static_cast,那么你的逻辑是错误的,因为类型与 static_cast 不兼容。
关于c++ - 错误 : invalid conversion from 'unsigned char*' to 'const signed char*' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39795088/