类型转换名称和语法

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的更多相关文章

  1. static_cast, dynamic_cast, const_cast 三种类型转化的区别

    强制转化四种类型可能很多人都常常忽略就象我一样,但是有时还是比较有用的.不了解的建议看看,一些机制我也不是十分了解,只是将一些用法写出来让大家看看.                           ...

  2. C++强制类型转换:static_cast、dynamic_cast、const_cast、reinterpret_cast

    1. c强制转换与c++强制转换 c语言强制类型转换主要用于基础的数据类型间的转换,语法为: (type-id)expression//转换格式1 type-id(expression)//转换格式2 ...

  3. C++的类型转换:static_cast、dynamic_cast、reinterpret_cast和const_cast

    在C++中,存在类型转换,通常意味着存在缺陷(并非绝对).所以,对于类型转换,有如下几个原则:(1)尽量避免类型转换,包括隐式的类型转换(2)如果需要类型转换,尽量使用显式的类型转换,在编译期间转换( ...

  4. static_cast、dynamic_cast、reinterpret_cast、和const_cast

    关于强制类型转换的问题,很多书都讨论过,写的最详细的是C++ 之父的<C++ 的设计和演化>.最好的解决方法就是不要使用C风格的强制类型转换,而是使用标准C++的类型转换符:static_ ...

  5. C++ ------ static_cast,dynamic_cast,reinterpret_cast,const_cast

    C++类型转换分为:隐式类型转换和显式类型转换 第1部分. 隐式类型转换 又称为“标准转换”,包括以下几种情况:1) 算术转换(Arithmetic conversion) : 在混合类型的算术表达式 ...

  6. C++里的强制类型转换符reinterpret_cast、static_cast 、dynamic_cast、const_cast 区别

    C 风格(C-style)强制转型如下: (T) exdivssion // cast exdivssion to be of type T 函数风格(Function-style)强制转型使用这样的 ...

  7. C++的类型转换:static_cast、dynamic_cast、reinterpret_cast和const_cast(dynamic_cast还支持交叉转换,const_cast将一个类的const、volatile以及__unaligned属性去掉)

    在C++中,存在类型转换,通常意味着存在缺陷(并非绝对).所以,对于类型转换,有如下几个原则:(1)尽量避免类型转换,包括隐式的类型转换(2)如果需要类型转换,尽量使用显式的类型转换,在编译期间转换( ...

  8. static_cast、dynamic_cast、reinterpret_cast、const_cast以及C强制类型转换的区别

    static_cast 1. 基础类型之间互转.如:float转成int.int转成unsigned int等 2. 指针与void*之间互转.如:float*转成void*.CBase*转成void ...

  9. static_cast dynamic_cast const_cast reinterpret_cast总结对比

    [本文链接] http://www.cnblogs.com/hellogiser/p/static_cast-dynamic_cast-const_cast-reinterpret_cast.html ...

随机推荐

  1. 论文答辩ppt要怎么写

    1.总体原则: 字大.字少.图多.要有重点 字体建议:正文要用黑体(如微软雅黑),标题可使用宋体或者黑体(如微软雅黑) 2. 主要分为两大部分: 2.1系统介绍 系统概述:概述自己系统主要是做了些什么 ...

  2. audio session config

    #pragma mark - #pragma mark - audio session config - (void)setAudioSessionConfig { NSError *error; A ...

  3. 集群技术(三)MySQL集群深度解析

    什么是MySQL集群 MySQL集群是一个无共享的(shared-nothing).分布式节点架构的存储方案,其目的是提供容错性和高性能. 数据更新使用读已提交隔离级别(read-committedi ...

  4. 用Netty解析Redis网络协议

    用Netty解析Redis网络协议 根据Redis官方文档的介绍,学习了一下Redis网络通信协议.然后偶然在GitHub上发现了个用Netty实现的Redis服务器,很有趣,于是就动手实现了一下! ...

  5. 支付平台PCI认证整改

    五大国际卡组织推出的PCI支付认证.支付平台改造方案.

  6. argparse库 学习记录

    初始化 始见参数 name or flags action nargs default type choices required help dest metavar 总结 继上次的optparser ...

  7. PHP Ajax JavaScript 实现 无刷新附件上传

    普通表单 前端页面 后台处理 带有文件的表单 刷新方式 前端界面 后台页面 无刷新方式 大文件上传 POST极值 upload极值 上传细节 前端页面 后台处理 总结 对一个网站而言,有一个基本的不可 ...

  8. Apache shiro集群实现 (六)分布式集群系统下的高可用session解决方案---Session共享

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  9. 使用Android Studio向GitHub提交代码

    使用Android Studio向GitHub提交代码 在GitHub上创建一个仓库 填写仓库名称.描述.类型(公有.私有)等信息,点击创建 到此,成功创建了我们的仓库,并且初始化创建了README. ...

  10. VisualStudio2010配置OpenCV的一种一劳永逸的方法

    用VS使用OpenCV的时候,又不想全局配置,每次都要配置简直烦死了. 鉴于此,给大家介绍一种简便一点的方法. 配置环境的前提是:成功安装了OpenCV合适的版本. 我这里用的是OpenCV2.4.9 ...