C/C++: C++ 中 static_cast 类型转换的含义
来源:互联网 发布:mac如何安装mysql 编辑:程序博客网 时间:2024/05/05 01:40
摘自: C++编程思想 : static_cast可以用于所有良定义转换。这些包括“安全”转换与次安全转换, “安全”转换 是编译器允许我们不用映射就能完成的转换。次安全转换也是良定义的。由 s t a t i c _ c a s t覆盖的变换的类型包括典型的无映射变换、窄化变换(丢失信息)用void*的强制变换、隐式类型变换和类层次的静态导航。
简单点说就是, static_cast 是温柔的,靠谱的,能做好的事情肯定能给你做,做不来的你别求他, 求也没用,别勉强。
#include <iostream>
using namespace std;
class base {};
class derived: public base
{
public:
operator int() {return 1;}
};
void func(int){}
class other {};
int main()
{
int i = 0x7fff;
long l;
float f;
l = i;
f = i;
l = static_cast<long>(i);
f = static_cast<float>(i);
i = l;
i = f;
i = static_cast<int>(l);
i = static_cast<char>(i);
char c = static_cast<char>(i); //warning
void *vp = &i;
float *fp = (float *)vp;
fp = static_cast<float *>(vp);
derived d;
base *bp = &d;
bp = static_cast<base*>(&d);
int x = d;
x = static_cast<int>(d);
func(d);
func(static_cast<int>(d));
derived* dp = static_cast<derived*>(bp);
//other *op = static_cast<other *>(bp);
other* op2 = (other*)bp;
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
- C/C++: C++ 中 static_cast 类型转换的含义
- C++static_cast类型转换函数
- c++ 类型转换(c cast static_cast dynamic_cast)
- static_cast与c风格的强制类型转换
- static_cast,reinterpret_cast及C的强制类型转换
- static_cast与c风格的强制类型转换比较
- C++:类型转换——static_cast和const_cast
- 浅析c++中的类型转换--static_cast
- 【C++】强制类型转换(static_cast,reinterpret_cast,const_cast,dynamic_cast,explicit)
- [C++] 强制类型转换static_cast、dynamic_cast、reinterpret_cast和const_cast
- c++dynamic_cast、const_cast 、static_cast、reinterpret_cast强制类型转换
- 【C#】中Decimal类型转换的问题
- C中常见的类型转换
- C++:static_cast
- <C/C++> 类型转换
- C/C++:类型转换
- C语言中类型转换
- 类型转换(c中)
- 快排
- UVA 12034 - Race(递推)
- 开源之静态站点生成器排行榜
- IOS学习之蓝牙4.0
- Maven使用教程
- C/C++: C++ 中 static_cast 类型转换的含义
- 名企笔试题
- linux平台kvm虚拟机
- 笔试
- 用C 写html文件
- JS实现div居中
- 《c++代码设计与重用》阅读笔记
- android用openGl ES绘制任意方程的三维空间图形
- 小米公司2013校园招聘笔试题(研发)+答案