C++ 多词类型的旧式cast
像C++ 多词类型的旧式cast,c++,C++,像int(x)这样的单字类型转换不会触发任何警告,但是对于像unsigned long、long long或unsigned char这样的多字类型,如何以类似的方式写入 类似于(unsigned long)(x)的内容将触发旧式强制类型转换警告 如果省略括号,如unsigned long(x),则是语法错误 假设变量x只是一些本机类型,如int,long,等等。。显然x的类型不是类或结构。您可以使用typedef: typedef unsigned long ulong; int i; un
int(x)
这样的单字类型转换不会触发任何警告,但是对于像unsigned long
、long long
或unsigned char
这样的多字类型,如何以类似的方式写入
类似于(unsigned long)(x)
的内容将触发旧式强制类型转换警告
如果省略括号,如unsigned long(x)
,则是语法错误
假设变量
x
只是一些本机类型,如int
,long
,等等。。显然x
的类型不是类或结构。您可以使用typedef
:
typedef unsigned long ulong;
int i;
unsigned long x= ulong(i);
这不是旧式强制转换警告,因为这不是强制转换,您正在从int
创建一个新的无符号long
您还可以使用static\u cast
:
unsigned long y= static_cast<unsigned long>(i);
unsigned long y=static_cast(i);
那是一个新的演员阵容