#include<stdio.h>voidstatic_cast_demo(){int i =0x12345;char c ='c';int* pi =&i;char* pc =&c;
c =static_cast<char>(i);//pc = static_cast<char*>(pi); //不能用于基本类型指针间转换
pc =reinterpret_cast<char*>(pi);}voidconst_cast_demo(){constint& j =1;int& k =const_cast<int&>(j);constint x =2;//真正意义上的常量int& y =const_cast<int&>(x);//无法去除只读属性,但分配空间命名y//int z = const_cast<int>(x);//const_cast只能用于引用和指针强转
k =5;printf("k = %d\n", k);//5printf("j = %d\n", j);//5
y =8;printf("x = %d\n", x);//8 x ---- 2printf("y = %d\n", y);//8printf("&x = %p\n",&x);//printf("&y = %p\n",&y);//不同地址 X ---- 与上面地址相同}voidreinterpret_cast_demo(){int i =0;char c ='c';int* pi =&i;char* pc =&c;
pc =reinterpret_cast<char*>(pi);
pi =reinterpret_cast<int*>(pc);
pi =reinterpret_cast<int*>(i);//c = reinterpret_cast<char>(i); //不能用于基本类型之间}voiddynamic_cast_demo(){int i =0;int* pi =&i;//char* pc = dynamic_cast<char*>(pi);//类 虚拟函数}intmain(){static_cast_demo();const_cast_demo();//reinterpret_cast_demo();//dynamic_cast_demo();return0;}