传智扫地僧课程学习笔记。
/* C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是: TYPE b = (TYPE)a C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用。 static_cast 静态类型转换。如int转换成char reinterpreter_cast 重新解释类型 dynamic_cast 命名上理解是动态类型转换。如子类和父类之间的多态类型转换。 const_cast, 字面上理解就是去const属性。 4种类型转换的格式: TYPE B = static_cast<TYPE> (a) */ double dpi = 3.1415926; int num = dpi;//隐式类型转换 int num1 = (int)dpi;//C类型转换 int num2 = static_cast<int>(dpi);//能做隐式的,静态一般都行,如果有错误会提示 char *p1 = "hello ...itcast"; int *p2 = NULL; //p2 = static_cast<int *>(p1);//error p2 = reinterpret_cast<int *>(p1);//有强制类型转换的味道, cout<< p1<<endl;//%s 打印出来的是字符串 cout<< p2<<endl;//%d 打印出来的是地址 /* C风格的强制类型转换(Type Cast)很简