c++类型转换

类型转换语法

C 风格的强制类型转换(Type Cast)很简单,不管什么类型的转换都是

TYPE b = (TYPE)a ;

c++风格转换提供了四种类型转换的操作符

  • static_cast                    静态类型转换。如int转换成char;
  • reinterpreter_cast        重新解释类型
  • dynamic_cast              名字上理解就是动态类型的转换,比如父类子类之前的多态类型转换
  • const_cast                   去除const属性;

以下是四种类型转换的格式:

TYPE B = static_cast<TYPE> (a) ;//其中static_cast可以换成其他的几种格式
 类型转换的介绍
  1. static_cast<>()            静态类型转换,编译时c++编译器会做类型检查,基本类型可以转,但是指针类型不可以转换
  2. reinterpreter_cast ()        一种较为底层的类型转换,它可以将一个指针转换为任意其他类型的指针,甚至可以将指针转换为整数类型。这种转换是非常灵活的,但也因此存在一定的潜在风险,尤其是在不同的平台上可能会导致移植性问题。(不推荐使用)
  3. dynamic_static()           动态类型转换,主要用于在继承关系中进行安全的向下转型(派生类到基类的转换)。它会进行运行时类型检查,确保转换的安全性。但 dynamic_cast<>() 只能用于含有虚函数的类层次结构中。
  4. const_cast<>()        用于去除常量性,将 const 修饰的对象转换为非常量版本。但需要注意,使用 const_cast<>() 进行类型转换时要确保操作的对象原本就是非常量,否则会导致未定义行为。
 static_cast<>() 和 reinterpreter_cast<>() 用法
#include<iostream>
using namespace std;
void main() {
	double pi = 3.1415926;

	//静态转换
	int num1 = static_cast<int>(pi);
	int num2 = (int)pi;//c语言  旧式类型转换
	int num3 = pi;//隐式类型转换
	cout << "num1 =" << num1 <<" "<< "num2 =" << num2 <<" "<< "num3 =" << num3<<endl;



	/*****************************************************************************/

	char *a = "wo shi da congming";
	int *a1 = NULL;
	a1 = (int*)a;
	//基本类型能转换,但不能转换指针类型
	//a1 = static_cast<int*> (a);//err:转换类型无效

	a1 = reinterpret_cast<int*>(a);
	cout << "a: " << a << endl;
	cout << "a1:" << a1 << endl;

	return ;
}
const_cast<>()用法
#include<iostream>
using namespace std;

void Opbuf(const char*p) {
	cout << p << endl;
	char *p2 = const_cast<char*>(p);
	p2[0] = 'b';
	cout << p << endl;
}

void main() {
	const char *p1 = "111111";
	char *p2 = const_cast<char*>(p1);//消除了p2的const
	char buf[100] = "aaaaa";
	cout << p2 << endl;
	Opbuf(buf);
	system("pause");
}
dynamic_cast 用法
#include<iostream>
using namespace std;
class Animal
{
public:
	virtual void cry() = 0;

private:

};
class Dog :public Animal {
public :
	virtual void cry() {
		cout << "wangwangwang" << endl;

	}
	void doswim() {
		cout << "我要叫" << endl;
	}
};
class Cat :public Animal {
public:
	virtual void cry() {
		cout << "iaomiaomiao" << endl;

	}
	void doTree() {
		cout << "爬树" << endl;

	}
};
//class Book
//{
//public:
//	void printP() {
//		cout << price << endl;
//	}
//private:
//	int price;
//};
void ObjPlay(Animal *base) {
	base->cry();
	Dog *pDog = dynamic_cast<Dog*>(base);
	if (pDog != NULL) {
		pDog->cry();
		pDog->doswim();
	}
	Cat *pCat = dynamic_cast<Cat*>(base);
	if (pCat != NULL) {
		pCat->cry();
		pCat->doTree();
	}
}
void main() {
	//Dog mydog;
	//Animal *base =&mydog;
	//base->cry();
	//Dog*pDog = static_cast<Dog*>(base);
	//Book *book = static_cast<Book*>(base);//err
	//Book *book = reinterpret_cast<Book*>(base);
	ObjPlay(new Cat());//dynamic_cast用法
	system("pause");
}

可以看看 C++的类型转换-腾讯云开发者社区-腾讯云这仅仅是分享我的笔记

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值