int main()
{
/*
*假设我们定义一个float a=5.6;
*int b=5;
*如果我把float强转为int的话,就会把小数点给截没了,我把b转为float的话
*5就会变为5.0。static_cast对隐式类型转换,即可以使用此类型
* b=static_cast<int>(a);
*/
#if 0
float a=5.6;
int b=5;
b=static_cast<int>(a);
#endif
/*
*但是也有单项的,比如void *p;int *q;你可以p=q;但是不能q=p;
*这个时候就用static_cast<int *>(p);
*/
#if 0
/*
* reintertrt_cast
*通常为操作数的位模式提供较低层的重新解释,也就是说将数据以二进制存在的形式
*的重新解释,在双方向上不可以隐式,则需要重新解释类型转换
*/
char *p;
int *q;
//p=q;这是不可以的
p=reinterpret_cast<char *>(q);
#endif
/*
*const_cast 目标类类型只能是指针或引用
*用来移除对象是常量性,使用const_cast去除const限定的目的不是为了修改它
*的内容,使用const_cast去除const限定,通常是为了函数能够接受这个实际的参数
*/
const int a=100;
int *p=const_cast<int *>(&a);
}
C++学习day7之类型转换
最新推荐文章于 2024-03-20 15:44:51 发布