Cpp-类型转换
static_cast: 安全转换,可用于子类向基类的显示转换,是编译期转换,没有运行时类型检查
dynamic_cast: 基类向派生类的安全转换,只能对指针或引用进行转换,是运行时转换
reinterpret_cast: 一些危险转换,只是将二进制的值拷贝过去,没有考虑类型之间的关系
const_cast: 去掉const
举例:
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| class A { public: int a; virtual void PrintA(){cout << "A\n";} };
class B { public: int b; virtual void PrintB(){cout << "B\n";} };
class C:public A, public B { public: virtual void PrintA(){cout << "AC\n";} virtual void PrintB(){cout << "BC\n";} };
int main() { C c; printf("%p %p %p\n", &c, reinterpret_cast<B*>(&c), static_cast<B*>(&c)); C* pc = new C; pc->a = 1; pc->b = 2; cout << reinterpret_cast<A*>(pc)->a << endl; cout << reinterpret_cast<B*>(pc)->b << endl; reinterpret_cast<A*>(pc)->PrintA(); reinterpret_cast<B*>(pc)->PrintB(); cout << dynamic_cast<A*>(pc)->a << endl; cout << dynamic_cast<B*>(pc)->b << endl; dynamic_cast<A*>(pc)->PrintA(); dynamic_cast<B*>(pc)->PrintB(); cout << static_cast<A*>(pc)->a << endl; cout << static_cast<B*>(pc)->b << endl; static_cast<A*>(pc)->PrintA(); static_cast<B*>(pc)->PrintB();
A* pa = new A; pa->a = 3; cout << reinterpret_cast<C*>(pa)->a << endl; cout << reinterpret_cast<C*>(pa)->b << endl; reinterpret_cast<C*>(pa)->PrintA(); reinterpret_cast<C*>(pa)->PrintB(); cout << dynamic_cast<C*>(pa)->a << endl; cout << dynamic_cast<C*>(pa)->b << endl; dynamic_cast<C*>(pa)->PrintA(); dynamic_cast<C*>(pa)->PrintB(); cout << static_cast<C*>(pa)->a << endl; cout << static_cast<C*>(pa)->b << endl; static_cast<C*>(pa)->PrintA(); static_cast<C*>(pa)->PrintB(); }
|