n=static_cast<int>(3.14);// n 的值变为 3 n=static_cast<int>(a);//调用 a.operator int,n 的值变为 1 p=static_cast<char*>(a);//调用 a.operator char*,p 的值变为 NULL n=static_cast<int>(p);//编译错误,static_cast不能将指针转换成整型 p=static_cast<char*>(n);//编译错误,...
用法:static_cast < type-id > ( expression ) 该运算符把expression转换为type-id类型,但没有运行时类型检查来保证转换的安全性。它主要有如下几种用法: ①用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换。 进行上行转换(把派生类的指针或引用转换成基类表示)是安全的; 进...
int n=9; double d = static_cast < double > (n); 上面的例子中, 我们将一个变量从 int 转换到 double。 这些类型的二进制表达式是不同的。 要将整数 9 转换到 双精度整数 9,static_cast 需要正确地为双精度整数 d 补足比特位。其结果为 9.0。 float f = 19.2F; unsigned int *pa = reinterpret...
光说不练不行,下面就写几个使用static_cast的应用代码。 示例1: 代码语言:javascript 复制 typedef unsigned charBYTE;voidf(){char ch;int i=65;float f=2.5;double dbl;ch=static_cast<char>(i);// int to chardbl=static_cast<double>(f);// float to doublei=static_cast<BYTE>(ch);} 示例代码...
enum e { A = 1, B, C }; double d = 12.25; unsigned int ui = 25; char c = static_cast<char>(ui); int i = static_cast<int>(d); int j = static_cast<int>(B); //父类子类转换 class F //father { public: int _father; ...
C++中的类型转换问题,其中就包括:dynamic_cast、static_cast、reinterpret_cast以及const_cast。
22. pa = reinterpret_cast<A*>(la); //la太长,只取低32位0x5678abcd拷贝给pa 23. unsigned int u = reinterpret_cast<unsigned int>(pa);//pa逐个比特拷贝到u 24. cout << hex << u << endl; //输出 5678abcd 25. typedef void (* PF1) (int); ...
static_cast<unsigned int> question... Jul 19 '05, 08:16 PM Hello, I'm (very) new to c++ and I'm having trouble understanding why this doesn't work. Here's some testcode: #include <iostream.h> int main() { float test;
reinterpret_cast不能转换掉表达式的const 可以用在将void*转换为int类型 代码语言:c++ 复制 unsigned short Hash( void *p ) { unsigned int val = reinterpret_cast<unsigned int>( p ); return ( unsigned short )( val ^ (val >> 16));
通常情况下,size_t其实就是unsigned int,是用typedef给unsigned int指定的别名。static_cast相当于隐式的类型转换,在这里,即把0转换成size_t类型。而,0是整形文字常量,它的类型是int型,所以,static_cast<size_t>(0)的意思就是把有符号的0转换成无符号的0。~运算符是按位取反运算符,也就...