9.四种转换类型

平时用的不多,但是比较重要的知识点

下面是概念框架和代码验证


#include <iostream>
using namespace std;
class A {
public:virtual ~A() {}
};
class B :public A {

};
void main()
{
	int a = 0,c = 2;
	const int *d = &c;
	double b = 1;
	int * p = &a;
	double * s = &b;
	a = static_cast<int>(b);

	c = reinterpret_cast<int>(p);		//获得p指针的地址
	s = reinterpret_cast<double*>(p);	//拷贝指针中的内容,如果类型不同,并不能用转换目标指针指示内容
	
	int * d1 = const_cast<int *>(d);	//去const		

	A * p1 = new A();
	B * p2 = new B();
	p2 = dynamic_cast<B*>(p1);			//向下转换
	p1 = dynamic_cast<A*>(p2);			//向上

	cout << a << *p << c << *s <<endl;
	system("pause");
}

结果并不重要,这里代码主要是示范用法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值