测试代码:
// ForbidChangeTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
struct mystruct
{
char char_1;
};
int _tmain(int argc, _TCHAR* argv[])
{
int IntNum =1;
long cLongNum = (long)IntNum;//c风格的强制类型转换
long LongNum = static_cast<long>(IntNum);//c++风格,基本的数据类型转换
int *pInt = &IntNum;
void *pVoid = static_cast<void*>(pInt); //任意类型指针转换成void类型指针
const mystruct mystruc;
//mystruc.char_1 ='a'; //错误
mystruct mystrut = const_cast<mystruct&>(mystruc);
mystrut.char_1 = 'a'; //正确
//dynamic_cast 动态类型转换,运行时检查,失败返回NULL,多态之间转换,基类中必须有虚函数
//reinterpret_cast 不同类型指针之间转换
return 0;
}