C++类型转化:static_cast,reinterpret_cast,dynamic_cast,const_cast
类型转换名称和语法
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)
demo 1
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
double pi = 3.1415926;
int num1 = (int)pi; // C类型转换
int num2 = static_cast<int>(pi); // 静态类型转换,编译的时候C++编译器会做类型检查
int num3 = pi; // C语言中隐式类型转换的地方,均可使用static_cast<>()进行类型转换
char *p1 = "hello";
int *p2 = NULL;
//p2 = static_cast<int *>(pi); // 使用static_cast,编译时编译器会报错
// 会做类型检查,若有错误,提示错误
p2 = reinterpret_cast<int *>(p1); // 若不同类型之间,进行强制类型转换,用reinterpret_cast<>()进行重新解释
cout << "p1: " << p1 << endl; // %s
cout << "p2: " << p2 << endl; // %d
/*
p1: hello
p2: 0127CA54
*/
return 0;
}
总结:
1)static_cast<>() 静态类型转换,编译的时c++编译器会做类型检查;
基本类型能转换 但是不能转换指针类型。
2)若不同类型之间,进行强制类型转换,用reinterpret_cast<>() 进行重新解释;
3)一般性结论:
C语言中 能隐式类型转换的,在c++中可用 static_cast<>()进行类型转换。因C++编译器在编译检查一般都能通过;
C语言中不能隐式类型转换的,在c++中可以用 reinterpret_cast<>() 进行强行类型。
解释。总结:static_cast<>()和reinterpret_cast<>() 基本上把C语言中的 强制类型转换给覆盖。
reinterpret_cast<>()很难保证移植性。
demo 2 - dynamic_cast<>(),动态类型转换,安全的基类和子类之间转换;运行时类型检查
#include <iostream>
#include <cstdio>
using namespace std;
class Tree {};
class Animal
{
virtual void cry() = 0;
};
class Dog : public Animal
{
public:
virtual void cry()
{
cout << "bowwow~" << endl;
}
void doHome()
{
cout << "look after the house!" << endl;
}
};
class Cat : public Animal
{
public:
virtual void cry()
{
cout << "meow" << endl;
}
void doThing()
{
cout << "catch a Mouse" << endl;
}
};
void playObj(Animal *base)
{
base->cry(); // 1有继承,2虚函数重载,3父类指针,指向子类对象。
// 会发生多态
// 识别子类对象
// dynamic_cast<>()运行时类型识别
Dog *pDog = dynamic_cast<Dog *>(base);
if (pDog != NULL) {
pDog->doHome();
}
Cat *pCat = dynamic_cast<Cat *>(base);
if (pCat != NULL) {
pCat->doThing();
}
}
int main()
{
Dog d1;
Cat c1;
Animal *pBase = NULL;
pBase = &d1;
pBase = static_cast<Animal *>(&d1); // 让C++编译器在编译的时候进行类型检查
// 强制类型转换
pBase = reinterpret_cast<Animal *>(&d1);
{
Tree t1;
//pBase = static_cast<Animal *>(&t1); // C++编译器做类型检查,报错
pBase = reinterpret_cast<Animal *>(&t1); // reinterpret_cast重新解释,这里有强制类型转换的感觉
}
playObj(&d1);
playObj(&c1);
return 0;
}
demo 3 - const_cast<>(),去除变量的只读属性
#include <iostream>
#include <cstdio>
using namespace std;
// const char *p的const修饰,让p指向的内存空间变成只读属性
void printBuf(const char *p)
{
//p[0] = 'Z'; // 报错
char *p1 = NULL;
// 程序员需要清楚的知道变量转换之前是什么类型,转换之后是什么类型
// const char * ==> char * 把只读属性去掉了
p1 = const_cast<char *>(p);
p1[0] = 'Z'; // 通过p1修改了内存空间
cout << p << endl;
}
int main()
{
char buf[] = "aaaaaaaasssssssssdddddddd";
printBuf(buf);
// 注意,要确保p所指向的内存空间确实能修改,如果不能修改,会带来灾难性后果
char *myp = "aaaaaadfdafaf"; // myp指向一块常量空间,这里是无论如何都不能被修改的
//printBuf(myp); // 运行直接奔溃
return 0;
}
总结
结论1:程序员要清除的知道: 要转的变量,类型转换前是什么类型,类型转换后是什么类型。转换后有什么后果。
结论2:一般情况下,不建议进行类型转换;避免进行类型转换。
C++类型转化:static_cast,reinterpret_cast,dynamic_cast,const_cast的更多相关文章
- static_cast, dynamic_cast, const_cast 三种类型转化的区别
强制转化四种类型可能很多人都常常忽略就象我一样,但是有时还是比较有用的.不了解的建议看看,一些机制我也不是十分了解,只是将一些用法写出来让大家看看. ...
- C++强制类型转换:static_cast、dynamic_cast、const_cast、reinterpret_cast
1. c强制转换与c++强制转换 c语言强制类型转换主要用于基础的数据类型间的转换,语法为: (type-id)expression//转换格式1 type-id(expression)//转换格式2 ...
- C++的类型转换:static_cast、dynamic_cast、reinterpret_cast和const_cast
在C++中,存在类型转换,通常意味着存在缺陷(并非绝对).所以,对于类型转换,有如下几个原则:(1)尽量避免类型转换,包括隐式的类型转换(2)如果需要类型转换,尽量使用显式的类型转换,在编译期间转换( ...
- static_cast、dynamic_cast、reinterpret_cast、和const_cast
关于强制类型转换的问题,很多书都讨论过,写的最详细的是C++ 之父的<C++ 的设计和演化>.最好的解决方法就是不要使用C风格的强制类型转换,而是使用标准C++的类型转换符:static_ ...
- C++ ------ static_cast,dynamic_cast,reinterpret_cast,const_cast
C++类型转换分为:隐式类型转换和显式类型转换 第1部分. 隐式类型转换 又称为“标准转换”,包括以下几种情况:1) 算术转换(Arithmetic conversion) : 在混合类型的算术表达式 ...
- C++里的强制类型转换符reinterpret_cast、static_cast 、dynamic_cast、const_cast 区别
C 风格(C-style)强制转型如下: (T) exdivssion // cast exdivssion to be of type T 函数风格(Function-style)强制转型使用这样的 ...
- C++的类型转换:static_cast、dynamic_cast、reinterpret_cast和const_cast(dynamic_cast还支持交叉转换,const_cast将一个类的const、volatile以及__unaligned属性去掉)
在C++中,存在类型转换,通常意味着存在缺陷(并非绝对).所以,对于类型转换,有如下几个原则:(1)尽量避免类型转换,包括隐式的类型转换(2)如果需要类型转换,尽量使用显式的类型转换,在编译期间转换( ...
- static_cast、dynamic_cast、reinterpret_cast、const_cast以及C强制类型转换的区别
static_cast 1. 基础类型之间互转.如:float转成int.int转成unsigned int等 2. 指针与void*之间互转.如:float*转成void*.CBase*转成void ...
- static_cast dynamic_cast const_cast reinterpret_cast总结对比
[本文链接] http://www.cnblogs.com/hellogiser/p/static_cast-dynamic_cast-const_cast-reinterpret_cast.html ...
随机推荐
- Docker常见仓库Ubuntu
Ubuntu 基本信息 Ubuntu 是流行的 Linux 发行版,其自带软件版本往往较新一些. 该仓库提供了 Ubuntu从12.04 ~ 14.10 各个版本的镜像. 使用方法 默认会启动一个最小 ...
- CMS垃圾收集器
介绍 CMS垃圾回收器的全称是Concurrent Mark-Sweep Collector,从名字上可以看出两点,一个是使用的是并发收集,第二个是使用的收集算法是Mark-Sweep.从而也可以推测 ...
- 这交互炸了(四) :一分钟让你拥有微信拖拽透明返回PhotoView
本文已授权微信公众号:鸿洋(hongyangAndroid)原创首发 <交互炸了>或许是一系列高端特效教程, 文中会介绍一些比较炫酷的特效,以及实现的思路.特效实现本身也许不会有太大的难度 ...
- Docker: Failed to get D-Bus connection: No connection to service
Issue: When you execute systemctl command in docker container, you may receive following error. Erro ...
- Android源码浅析(六)——SecureCRT远程连接Linux,配置端点和字节码
Android源码浅析(六)--SecureCRT远程连接Linux,配置端点和字节码 需要编译源码的同学,一般都是win+虚拟机吧,但是再虚拟机里体验并不是很好,所有市面上有很多的软件能够做到在wi ...
- Struts 1 之文件上传
Struts 1 对Apache的commons-fileupload进行了再封装,把上传文件封装成FormFile对象 定义UploadForm: private FormFilefile; //上 ...
- Android图片setBackgroundResource和setImageResource的区别
网上对于这2个的区别大都是如下所示: setImageResource與xml中的src的屬性才是相匹配的,而setBackgroundResource是與xml中的background屬性相匹配 的 ...
- java虚拟机参数设置 jvm参数设置
java进程命令行使用方式如下: java [-options] class [args...] -options 表示虚拟机的启动参数, class为带有main()函数的java类的全名称 arg ...
- Java中Excel导入功能实现、excel导入公共方法_POI -
这是一个思路希望能帮助到大家:如果大家有更好的解决方法希望分享出来 公司导入是这样做的 每个到导入的地方 @Override public List<DataImportMessage> ...
- [shiro学习笔记]第二节 shiro与web融合实现一个简单的授权认证
本文地址:http://blog.csdn.net/sushengmiyan/article/details/39933993 shiro官网:http://shiro.apache.org/ shi ...