对C++中的类型转换感到困惑

Ven*_*kat 0 c++ types type-conversion

在C++中,以下几行让我困惑:

int temp = (int)(0×00);

int temp = (0×00int);
Run Code Online (Sandbox Code Playgroud)

这两行之间有什么区别?

Mar*_*ers 12

两者都无效,因为您使用的是×代替x:

test.cpp:6: error: stray '\215' in program
test.cpp:6: error: expected primary-expression before "int"
test.cpp:6: error: expected `)' before "int"
Run Code Online (Sandbox Code Playgroud)

但即便修复,第二个仍然是无效的C++,因为你不能写0x00int:

test.cpp:6:13: invalid suffix "int" on integer constant
Run Code Online (Sandbox Code Playgroud)

第一个是有效的(在更改×为之后x)并将值0赋给temp.这里不需要强制转换 - 你不需要强制转换只是因为常量是用十六进制编写的.你可以写:

int temp = 0x00;
Run Code Online (Sandbox Code Playgroud)