复制 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);} 示例代码2: 代码语言:javascript 复制 // static_cast_Operator_2.cpp// compile with: /LD...
您应该使用reinterpret_cast<char *>而不是static_cast<char *>,因为数据类型不相关:例如,您可以在指向子类的指针与超类之间转换,或者在int之间转换和long,或void *与任何指针之间,但unsigned int *到char *不是“安全”,因此您无法使用static_cast。< / p> 不同之处在于,在C ++中,您有各种类型的强制转换:...
uint8_t是 C++ 中的一个标准整数类型,表示一个无符号 8 位整数(通常对应于unsigned char)。 uint8_t*是指向uint8_t类型的指针。 3.static_cast<uint8_t*>的具体用途 这个表达式的含义是:将某个表达式(通常是一个指针类型)转换为uint8_t*类型。它要求: 源类型与目标类型之间存在逻辑上的可转换性(例如,...
// 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 char dbl = static_cast<double>(f); // float to double i = static_cast<BYTE>(ch); ...
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 long*和unsigned char*是不相关的指针类型,因此不能在它们之间使用static_cast,需要使用...
3) reinterpret_cast:指针类型和整型或其他指针间不安全的相互转换,仅在你对所做一切了然于心时使用;(主要用于指针间的转换, 例如 const void* 与const char*间的转换, 又或者const unsigned char* 与 const char*间的转换) 4) dynamic_cast:除测试外不要使用,除单元测试外,如果你需要在运行时确定类型信息,...
在前一则教程中,我们阐述了多态的相关概念,其中就包括实现多态所必须的虚函数,以及使用多态这个性质时...
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; ...
所以是不安全的。 ②用于基本数据类型之间的转换,如把int转换成char,把int转换成enum。这种转换的安全性也要开发人员来保证。 ③把空指针转换成目标类型的空指针。 ④把任何类型的表达式转换成void类型。 注意:static_cast不能转换掉expression的const、volitale、或者__unaligned属性。