类型转换名称和语法

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. 设计模式一日一练:中介者模式(Mediator)

    Mediator模式,用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式的相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 比较典型的例子是联合国.QQ群等.比如,如果中国有 ...

  2. 虚拟机克隆,并设置新的ip

    6.1克隆新的虚拟机 选中某个虚拟机-à右键à管理à克隆 选择下一步 选择下一步 点击完成 6.2修改主机名 [root@hadoop3 ~]# vim/etc/sysconfig/network 将 ...

  3. Switch控件详解

    Switch控件详解 原生效果 5.x 4.x 布局 <Switch android:id="@+id/setting_switch" android:layout_widt ...

  4. CDH集群安装&测试总结

    0.绪论 之前完全没有接触过大数据相关的东西,都是书上啊,媒体上各种吹嘘啊,我对大数据,集群啊,分布式计算等等概念真是高山仰止,充满了仰望之情,觉得这些东西是这样的: 当我搭建的过程中,发现这些东西是 ...

  5. SceneKit一个3D场景角色的代码重构

    SuperSpaceMan3D是一个以SceneKit为基础的小游戏项目,作者展示了用SceneKit开发3D游戏的强大威力.不过在实际运行时会发现有一些小bug,这里我们依次尝试将其修复 首先,当s ...

  6. Dynamics CRM2013 6.1.1.1143版本插件注册器的一个bug

    最近在做的项目客户用的是CRM2013sp1版本,所以插件注册器使用的也是与之对应的6.1.1.1143,悲剧的事情也因此而开始. 在插件中注册step时,工具里有个run in user's con ...

  7. 深入了解UIViewController控制器与对应的View类的详解

    ViewController是iOS开发中MVC模式中的C(视图控制器),ViewController是view的controller,ViewController的职责主要包括管理内部各个view的 ...

  8. Nginx的负载均衡 - 加权轮询 (Weighted Round Robin) 上篇

    Nginx版本:1.9.1 我的博客:http://blog.csdn.net/zhangskd 算法介绍 来看一个简单的Nginx负载均衡配置. http { upstream cluster { ...

  9. android连接打印机

    android连接  网络打印,主要使用socket连接设备,发送指令给设备. 首先要有设备的IP,端口号一般默认的是9100 //打印设备网络IP etIp.setText("192.16 ...

  10. TCP连接建立系列 — 客户端的端口选取和重用

    主要内容:connect()时的端口选取和端口重用. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 端口选取 connect()时本地端口是如何选取的呢 ...