繁体   English   中英

错误:从'unsigned char *'到'constsigned char *'的无效转换

[英]error: invalid conversion from 'unsigned char*' to 'const signed char*'

uint8_t* buf1;
...
const signed char* buf2 = static_cast<const signed char*>(buf1);

从类型'uint8_t * {aka未签名字符*}'到类型'const签名字符*'的无效static_cast

c-style-cast:(constsigned char *)工作正常

在这种情况下,使用c-style-cast和static_cast有危险吗?

在这种情况下,使用c-style-cast和static_cast有危险吗?

如错误消息所述,在这种情况下,根本不能使用static_cast。

使用c样式强制转换的危险是您可能不打算执行reinterpret_cast,这正是c样式强制转换在这里执行的。 如果您打算执行reinterpret_cast,请使用reinterpret_cast。 如果打算使用static_cast,则您的逻辑是错误的,因为类型与static_cast不兼容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM