c++ - 错误 : invalid conversion from 'unsigned char*' to 'const signed char*'

标签 c++ c++11 casting

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/

相关文章:

c++ - 使用模板转换特定类型的参数

c++ - push_backs 到 std::vector<std::reference_wrapper<type>>

c++ - 这是 C++11 中的 struct POD 吗?

java - 将 Int 转换为 Java 中的枚举

c++ - 如何在 C++11 constexpr 中检查 double 的位模式是 0x0?

c++ - Windows 设备属性文档?

c++ - 如何检查mpl::vector_c中的值?

c# - 将 Unsigned Long 转换为 ascii 字符串

c++ - move 构造函数如何在 C++ 中工作?

c++ - 为什么将类型 T& 转换为 T 会创建一个临时对象?