如何处理来自“const char *”的翻译to“const unsigned char *”? [英] how to deal with the translation from "const char * " to "const unsigned char *"?

查看:66
本文介绍了如何处理来自“const char *”的翻译to“const unsigned char *”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写道:

------------------------------------ -----------------------------------

------- ---------------------------------

unsigned char * p = reinterpret_cast< unsigned char *>(" abcdg");

sizeof(reinterpret_cast< const char *>(p));

---------- -------------------------------------------------- -----------

------------------------------- ---------


编译器告诉我reinterpret_cast from typeconst char *"

to type" unsigned char *抛弃constness"

解决方案




1月28日晚上11:21 ,?1 ?? 1 ??¤§?¤???? < chendong ... @ gmail.comwrote:


i写道:--------------------- --------------------------------------------------

----------------------------------------

unsigned char * p = reinterpret_cast< unsigned char *>(" abcdg");

sizeof(reinterpret_cast< const char *>(p));

--------------------------------------------- --------------------------

---------------- ------------------------


编译器告诉我reinterpret_cast from typeconst char *

键入unsigned char *抛弃了constness



此时你停止与C ++废话摔跤,只需使用

C风格的演员。


好​​的旧C风格演员将隐式生成reinterpret_cast和static_cast的正确

组合,而没有措辞

使你的代码混乱。


由于没有涉及其含义可能会改变的typedef名称,因此C

样式的演员阵容不比新的演员组合更安全,并且是

更具可读性。


(如果你使用抽象类型的C演员,比如把一些A *演绎到一些B *,那么就有危险就是有人可能会重新定义A和B是什么,并且演员将默默地继续工作,即使改变可能是b $ b,这样转换会产生错误的后果)。 />


?1【1】¤§?¤????写道:


i写道:

---------------------- -------------------------------------------------

----------------------------------------

unsigned char * p = reinterpret_cast< unsigned char *>(" abcdg");

sizeof(reinterpret_cast< const char *>(p));

---------------------------------------------- -------------------------

----------------- -----------------------


编译器告诉我reinterpret_cast from typeconst char *

键入unsigned char *抛弃constness"



编译器是对的。 unsigned char *是非常量和字符串

文字(如abcdg)是常量。

如果你要转换为unsigned char *你需要问问自己为什么。

演员之后会修改吗? (这将是未定义和吐司

您在某些平台上的过程)


看起来您有设计问题。


?????写道:


i写道:

---------------------- -------------------------------------------------

----------------------------------------

unsigned char * p = reinterpret_cast< unsigned char *>(" abcdg");

sizeof(reinterpret_cast< const char *>(p));

---------------------------------------------- -------------------------

----------------- -----------------------


编译器告诉我reinterpret_cast from typeconst char *

键入unsigned char *抛弃了constness



在您发布的代码中,您不会转换为const unsigned char *。尝试:


unsigned char const * p = static_cast< unsigned char const *>(" abcdg");

Best


Kai-Uwe Bux


i wrote:
-----------------------------------------------------------------------
----------------------------------------
unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");
sizeof(reinterpret_cast<const char *>(p));
-----------------------------------------------------------------------
----------------------------------------

the compiler tells me that "reinterpret_cast from type "const char * "
to type "unsigned char *" casts away constness "

解决方案



On Jan 28, 11:21 pm, "?1??1??¤§?¤????" <chendong...@gmail.comwrote:

i wrote:-----------------------------------------------------------------------
----------------------------------------
unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");
sizeof(reinterpret_cast<const char *>(p));
-----------------------------------------------------------------------
----------------------------------------

the compiler tells me that "reinterpret_cast from type "const char * "
to type "unsigned char *" casts away constness "

At this point you stop wrestling with the C++ bullshit and just use a
C style cast.

The good old C style cast will implicitly produce the correct
combination of reinterpret_cast and static_cast, without the verbiage
cluttering your code.

Since no typedef names are involved whose meanings could change, the C
style cast is no less safe than the new style cast combination, and is
much more readable.

(If you use the C cast with abstracted types, like casting some A * to
some B *, the danger is that someone may redefine what A and B is, and
the cast will silently continue to work, even though the change may be
such that the conversion has erroneous consequences).


?1??1??¤§?¤???? wrote:

i wrote:
-----------------------------------------------------------------------
----------------------------------------
unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");
sizeof(reinterpret_cast<const char *>(p));
-----------------------------------------------------------------------
----------------------------------------

the compiler tells me that "reinterpret_cast from type "const char * "
to type "unsigned char *" casts away constness "


The compiler is right. "unsigned char *" is non-const and string
literals (like "abcdg") are const.
If you''re casting to a "unsigned char *" you need to ask yourself why.
Will it be modified after the cast ? (that would be undefined and toast
your process on some platforms)

It seems like you have a design problem.


????? wrote:

i wrote:
-----------------------------------------------------------------------
----------------------------------------
unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");
sizeof(reinterpret_cast<const char *>(p));
-----------------------------------------------------------------------
----------------------------------------

the compiler tells me that "reinterpret_cast from type "const char * "
to type "unsigned char *" casts away constness "

In the code you posted, you do not cast to "const unsigned char *". Try:

unsigned char const * p = static_cast<unsigned char const *>("abcdg");
Best

Kai-Uwe Bux


这篇关于如何处理来自“const char *”的翻译to“const unsigned char *”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆