您应该使用reinterpret_cast<char *>而不是static_cast<char *>,因为数据类型不相关:例如,您可以在指向子类的指针与超类之间转换,或者在int之间转换和long,或void *与任何指针之间,但unsigned int *到char *不是“安全”,因此您无法使用static_cast。< / p> 不同之处在于,在C ++中,您有各种类型的强制转换:...
static_cast can be used to convert from an int to a char. However, the resulting char may not have enough bits to hold the entire int value. Again, it is left to the programmer to ensure that the results of a static_cast conversion are safe. ...
intmain(){D*pD=newD;B*pB=dynamic_cast<B*>(pD);A*pA=dynamic_cast<A*>(pB);} 这就是我在实现QueryInterface时,得到IUnknown的指针时,使用的是*ppv = static_cast<IX *>(this);而不是*ppv = static_cast<IUnknown *>(this); 对于多重继承的情况,从派生类往父类的父类进行转时,需要特别注意;...
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 ...
在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全。 class B{ public: int m_iNum; virtual void foo(); }; class D:public B{ public: char *m_szName[100]; }; void func(B *pb){ D *pd1 = static_cast(pb);
(static_cast,dynamic_cast,const_static,reinterpret_cast) 1 static_cast (1)用于基本的数据类型转换(char,int),及指针之间的转换 test_enum type = test_enum_1; 1. char a ; 1. int b = static_cast<int>(a); 1.
静态类型转换关键字,是 C++ 编译器的新特性,C 编译器不支持这个关键字的;主要用于将表达式转换成另一个数据类型,这个转换会经过编译器的合法性检查;
static_cast (f);此时结果,i的值为166。2.reinterpret_cast(重述转换)主要是将数据从一种类型的转换为另一种类型。所谓“通常为操作数的位模式提供较低层的重新解释”也就是说将数据以二进制存在形式的重新解释。比如:int i;char p = "this is a example.";i = reinterpret_cast (p);此时...
int b = static_cast<int>(a);//正确,将char型数据隐式转换成int型数据 double *c = new double; void *d = static_cast<void*>(c);//正确,将double指针转换成void指针 int e = 10; const int f = static_cast<const int>(e);//正确,将int型数据转换成const int型数据 ...