C++11新特性——类型转换(二)

C++11新特性(二)

C++类型转换

新风格:TYPE b = 类型操作符(a)

类型操作符 = static_cast | reinterpreter_cast | dynamic_cast | const_cast

1、static_cast

静态类型转换(斯文的劝导,温柔的转换)

#include <iostream>

using namespace std;

class Animal {
public:
	virtual void cry() = 0;
};
class Cat :public Animal {
public:
	void cry() {
		cout << "喵喵" << endl;
	}
};
class Dog :public Animal {
public:
	void cry() {
		cout << "汪汪" << endl;
	}
};

int main(void) {

	//第一种情况: 父子类之间的类型转换
	Dog* dog = new Dog();
	Animal* a1 = static_cast<Animal*> (dog); //子类指针->父类指针 上行指针

	Dog* dog1_1 = static_cast<Dog*> (a1);//父类的指针转型到子类的指针
	Cat* cat1 = static_cast<Cat*>(a1);//父类到子类  这样不行,有隐患 狗 -> 动物 -> 猫?


	Dog dog2;
	Animal& a2 = static_cast<Animal&> (dog2);//子类的引用转型到父类的引用

	Dog& dog2_2 = static_cast<Dog&>(a2);//父类到子类引用
	
	//第二种 基本类型的转换
	int kk = 234;
	char cc = static_cast<char>(kk);

	//第三种 把空指针转换成目标类型的空指针
	int* p = static_cast<int*>(NULL);
	Dog* dp = static_cast<Dog*>(NULL);
	
	//第四种 把任何类型的表达式转换成void类型
	int* p1 = new int[10];
	void* vp = static_cast<void*> (p1);

}

2、reinterpret_cast

重新解释类型(挂羊头卖狗肉)不同类型间的互转,数值与指针的互转

TYPE b = reinterpret_cast<TYPE>(a)

TYPE必须是一个指针、引用、算术类型、函数指针

滥用reinterpret_cast运算符可能很容易带来风险。除非所需转换本身是低级别的,否则应使用其他强制类型转换运算符之一。

#include <iostream>

using namespace std;

class Animal {
public:
	virtual void cry() {
		cout << "动物叫" << endl;
	}
};
class Cat :public Animal {
public:
	void cry() {
		cout << "喵喵" << endl;
	}
};
class Dog :public Animal {
public:
	void cry() {
		cout << "汪汪" << endl;
	}
};

int main(void) {

	//用法一  数值与指针之间的转换
	int* p = reinterpret_cast<int*>(0x999999);
	int val = reinterpret_cast<int>(p);

	//用法二 不同类型指针和引用之间的转换
	Dog dog1;
	Animal* a1 = &dog1;
	a1->cry();

	Dog* dog1_p = reinterpret_cast<Dog*>(a1);
	Dog* dog2_p = static_cast<Dog*>(a1); //如果能用static_cast,static_cast优先

	Cat* cat1_p = reinterpret_cast<Cat*>(a1);
	//Cat* cat2_p = static_cast<Cat*>(dog1_p);//不同类型指针转换不能 使用static_cast
	Cat* cat2_p = reinterpret_cast<Cat*>(dog1_p);//不同类型指针转换不能 使用static_cast

	Animal& a2 = dog1;
	Dog& dog3 = reinterpret_cast<Dog&>(a2);//引用强转用法

	dog1_p->cry();
	dog2_p->cry();

	cat1_p->cry();
	cat2_p->cry();

	system("pause");
	return 0;
}

3、dynamic_cast

动态类型转换:

  • 将一个基类对象指针cast到继承类指针dynamic_cast会根据基类指针是否真正指向继承指针来做相应处理。失败返回null,成功返回正常cast后的对象指针;
  • **将一个基类对象引用cast继承类对象,dynamic_cast会根据基类对象是否真正属于继承类来做相应处理,失败抛出异常bad_cast **
  • dynamic_cast在将父类cast到子类时,父类必须要有虚函数一起玩
void animalPlay(Animal* animal) {
	animal->cry();
	//基类指针是否真正指向继承指针来做相应处理。失败返回null,成功返回正常cast后的对象指针;
	Dog* pDog = dynamic_cast<Dog*>(animal);
	if (pDog) {
		pDog->play();
	}
	else {//pDog==NULL
		cout << "不是狗,别骗我!" << endl;
	}
}

int main(void) {
	/*
	汪汪
	溜达溜达
	喵喵
	不是狗,别骗我!
	*/
	Dog* dog1 = new Dog();
	Animal* a1 = dog1;
    
	animalPlay(a1);

	Cat* cat1 = new Cat();
	Animal* a2 = cat1;

	animalPlay(a2);


	system("pause");
	return 0;
}
void animalPlay1(Animal& animal) {
	animal.cry();
	try {
		Dog& pDog = dynamic_cast<Dog&>(animal);
		pDog.play();
	}
	catch(std::bad_cast bc){
		cout << "不是狗,那应该是猫" << endl;
	}
	try {
		Cat& pCat = dynamic_cast<Cat&>(animal);
		pCat.play();
	}
	catch (std::bad_cast bc) {
		cout << "不是猫,那应该是上面的狗" << endl;
	}
}
Cat* cat1 = new Cat();
Animal* a2 = cat1;

animalPlay1(*a2);
/*
喵喵
不是狗,那应该是猫
爬爬树
*/

4、const_cast

取const属性(仅针对于指针和引用)

#include <iostream>

using namespace std;

void demo(const char* p) {
	//p[0] = 'A';//不允许修改,但是我必须要修改?
	/*
	char* p1 = const_cast<char*>(p);//可以转了
	p1[0] = 'A';
	*/
	//直接去掉const修改
	const_cast<char*>(p)[0] = 'A';//和上面效果一样
	cout << p << endl;
	//A23456789
}

int main(void) {
	//字符串数组
	char p[] = "123456789";
	demo(p);

	//常量字符串 不能去掉const修改
	//警告:在去掉常量限定符之前,保证指针所指向的内存能够修改,不能则会引起异常。
	const char* cp = "987654321";
	demo(cp);

	system("pause");
	return 0;
}

类型转换使用建议

  • static_cast静态类型转换,编译的时c++编译器会做编译时的类型检查;隐式转换;

基本类型转换,父子类之间合理转换

  • 若不同类型之间,进行强制类型转换,用reinterpret_cast<>() 进行重新解释

建 议:

​ C语言中能隐式类型转换的,在c++中可用 static_cast<>()进行类型转换。因C++编译器在编译检查一般都能通过;C语言中不能隐式类型转换的,在c++中可以用 `reinterpret_cast<>() 进行强制类型解释。

​ 总结:static_cast<>()reinterpret_cast<>() 基本上把C语言中的 强制类型转换给覆盖,注意reinterpret_cast<>()很难保证移植性。

  • dynamic_cast<>()动态类型转换,安全的虚基类和子类之间转换;运行时类型检查

  • const_cast<>(),去除变量的只读属性

最后的忠告:程序员必须清楚的知道: 要转的变量,类型转换前是什么类型,类型转换后是什么类型,转换后有什么后果

C++大牛建议:一般情况下,不建议进行类型转换;避免进行类型转换。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值