记录C++语法的点点滴滴
1.新式类型转换
static_cast |
用于兼容类型转换,如整型和浮点型、字符型之间的互相转换。 |
const_cast |
仅用于进行去除 const 属性的转换 |
reinterpret_cast |
用于进行各种不同类型的指针之间、不同类型的引用之间以及指针和能容纳指针的整数类型之间的转换 |
dynamic_cast |
专门用于将多态基类的指针或引用强制转换为派生类的指针或引用,而且能够检查转换的安全性。对于不安全的指针转换,转换结果返回 NULL 指针 |
xxxxxx_cast<T2>T1
:
- 语法:
xxxxxx_cast<类型2>(类型1变量)
- 功能:将指针变量由类型1转换为类型2
1 2 3 4 5 6
| static_cast<const uchar *>(p) static_cast<size_t>(p) const_cast<uchar *>(p) reinterpret_cast<const uchar *>(p);
|
1.1 static_cast
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <iostream> using namespace std; class A { public: operator int() { return 1; } operator char*() { return NULL; } }; int main() { A a; int n; char* p = "New Dragon Inn"; n = static_cast <int> (3.14); n = static_cast <int> (a); p = static_cast <char*> (a); n = static_cast <int> (p); p = static_cast <char*> (n); return 0; }
|
1.2 reinterpret_cast
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include <iostream> using namespace std; class A { public: int i; int j; A(int n):i(n),j(n) { } }; int main() { A a(100); int &r = reinterpret_cast<int&>(a); r = 200; cout << a.i << "," << a.j << endl; int n = 300; A *pa = reinterpret_cast<A*> ( & n); pa->i = 400; pa->j = 500; cout << n << endl; long long la = 0x12345678abcdLL; pa = reinterpret_cast<A*>(la); unsigned int u = reinterpret_cast<unsigned int>(pa); cout << hex << u << endl; typedef void (* PF1) (int); typedef int (* PF2) (int,char *); PF1 pf1; PF2 pf2; pf2 = reinterpret_cast<PF2>(pf1); }
|
1.3 const_cast
示例
1.4 dynamic_cast
示例
reinterpret_cast
可以将多态基类(包含虚函数的基类)的指针强制转换为派生类的指针,但是这种转换不检查安全性,即不检查转换后的指针是否确实指向一个派生类对象。dynamic_cast
专门用于将多态基类的指针或引用强制转换为派生类的指针或引用,而且能够检查转换的安全性。
如果类型转换成功,则 dynamic_cast
返回 新类型
的值。若转换失败且新类型
是指针类型,则它返回该类型的空指针。若转型失败且新类型
是引用类型,则它抛出与类型 std::bad_cast
的处理块匹配的异常。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #include <iostream> struct V { virtual void f() {}; }; struct A : virtual V {}; struct B : virtual V { B(V* v, A* a) { dynamic_cast<B*>(v); dynamic_cast<B*>(a); } }; struct D : A, B { D() : B((A*)this, this) { } }; struct Base { virtual ~Base() {} }; struct Derived: Base { virtual void name() {} }; int main() { D d; A& a = d; D& new_d = dynamic_cast<D&>(a); B& new_b = dynamic_cast<B&>(a); Base* b1 = new Base; if(Derived* d = dynamic_cast<Derived*>(b1)) { std::cout << "downcast from b1 to d successful\n"; d->name(); } Base* b2 = new Derived; if(Derived* d = dynamic_cast<Derived*>(b2)) { std::cout << "downcast from b2 to d successful\n"; d->name(); } delete b1; delete b2; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| #include <iostream> #include <string> using namespace std; class Base { public: virtual ~Base() {} }; class Derived : public Base { }; int main() { Base b; Derived d; Derived* pd; pd = reinterpret_cast <Derived*> (&b); if (pd == NULL) cout << "unsafe reinterpret_cast" << endl; pd = dynamic_cast <Derived*> (&b); if (pd == NULL) cout << "unsafe dynamic_cast1" << endl; pd = dynamic_cast <Derived*> (&d); if (pd == NULL) cout << "unsafe dynamic_cast2" << endl; return 0; }
|