C++中的类型转换问题,其中就包括:dynamic_cast、static_cast、reinterpret_cast以及const_cast。
用法:static_cast < type-id > ( expression ) 该运算符把expression转换为type-id类型,但没有运行时类型检查来保证转换的安全性。它主要有如下几种用法: ①用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换。 进行上行转换(把派生类的指针或引用转换成基类表示)是安全的; 进...
unsignedint u=reinterpret_cast<unsignedint>(pa);//pa逐个比特拷贝到u cout<< hex<< u<< endl;//输出 5678abcd typedefvoid(* PF1)(int); typedefint(* PF2)(int,char*); PF1pf1;PF2pf2; pf2=reinterpret_cast<PF2>(pf1);//两个不同类型的函数指针之间可以互相转换 } 程序的输出结果是: 20...
std::cout << std::hex <<static_cast<int>(rawData[i]) <<" "; } 在此示例中: &data是一个Data*类型的指针。 通过static_cast<uint8_t*>将其转换为字节指针,以便逐字节操作。 5.static_cast的限制 static_cast不允许不安全的转换,例如从uint8_t*转换为不相关类型的指针(需要使用reinterpret_cast)...
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; }; class S : public F //son { public: _son; ...
> unsigned int test2; > test = 1.8; > cout << "\n" << test*100.0 << endl; > test2 = static_cast<uns igned int>(test*100.0 ); > cout << "\n" << test2 << endl; > > } > > The strange thing is that the program returns 180 on the first cout[/color] ...
光说不练不行,下面就写几个使用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);} ...
static_cast不能从整数强制转换为指针,反之亦然。也不能在两个不相关的类型之间强制转换。unsigned long...
Original issue 35 created by Alexander.Klishin on 2011-08-23T06:31:19.000Z: util/coding_test may fail on some BIG_ENDIAN platforms uname -ms HP-UX ia64 gcc version 4.3.3 (GCC) micro test: ===cast.cpp === include <stdio.h> int main(int ...
23. unsigned int u = reinterpret_cast<unsigned int>(pa);//pa逐个比特拷贝到u 24. cout << hex << u << endl; //输出 5678abcd 25. typedef void (* PF1) (int); 26. typedef int (* PF2) (int,char *); 27. PF1 pf1; PF2 pf2; ...