作用是把time函数返回的值转换成unsigned int型值 而这里的static_cast就是C++风格的类型转换
pa=reinterpret_cast<A*>(la);//la太长,只取低32位0x5678abcd拷贝给pa 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>(...
用法:static_cast < type-id > ( expression ) 该运算符把expression转换为type-id类型,但没有运行时类型检查来保证转换的安全性。它主要有如下几种用法: ①用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换。 进行上行转换(把派生类的指针或引用转换成基类表示)是安全的; 进...
IBM的C++指南、C++之父Bjarne Stroustrup的FAQ网页和MSDN的Visual C++也都指出:错误的使用reinterpret_cast很容易导致程序的不安全,只有将转换后的类型值转换回到其原始类型,这样才是正确使用reinterpret_cast方式。 MSDN中也提到了,实际中可将reinterpret_cast应用到哈希函数中,如下(64位系统中需将unsigned int修改为unsig...
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); ...
C++中的类型转换问题,其中就包括:dynamic_cast、static_cast、reinterpret_cast以及const_cast。
IBM的C++指南、C++之父也都指出:错误的使用reinterpret_cast很容易导致程序的不安全,只有将转换后的类型值转换回到其原始类型,这样才是正确使用reinterpret_cast方式。 MSDN中也提到了,实际中可将reinterpret_cast应用到哈希函数中,如下(64位系统中需将unsigned int修改为unsigned long): ...
通常情况下,size_t其实就是unsigned int,是用typedef给unsigned int指定的别名。static_cast相当于隐式的类型转换,在这里,即把0转换成size_t类型。而,0是整形文字常量,它的类型是int型,所以,static_cast<size_t>(0)的意思就是把有符号的0转换成无符号的0。~运算符是按位取反运算符,也就...
光说不练不行,下面就写几个使用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 运算符还可用于执行任何隐式转换,包括标准转换和用户定义的转换。 例如:C++ 复制 // static_cast_Operator_3.cpp // compile with: /LD /GR typedef unsigned char BYTE; void f() { char ch; int i = 65; float f = 2.5; double dbl; ch = static_cast<char>(i); // int to ...