C++11新增类型转换

一、静态类型转换:static_cast

#include<Windows.h>
#include<iostream>
using namespace std;
class A
{
public:
	virtual void Show() {
		cout << "基类:A\n";
	}
	void role() {
		cout << "啥也不干!\n";
	}
};
class B :public A
{
public:
	void Show() {
		cout << "派生类:B\n";
	}
	void role() {
		cout << "哈哈哈\n";
	}
};
class C :public A
{
public:
	void Show() {
		cout << "派生类:C\n";
	}
	void role() {
		cout << "啦啦啦\n";
	}
};

int main(void)
{
	A a;
	B b;
	C c;
	A* ap = &b;
	/****************************************************
	静态类型转换:static_cast
	仅当允许转换的时候,才可以用,取代隐式转换
	****************************************************/
	B* bp = static_cast<B*>(ap);
	bp->role();
	system("pause");
	return 0;
}

二、重新解释:reinterpret_cast

#include<Windows.h>
#include<iostream>
using namespace std;
class A
{
public:
	virtual void Show() {
		cout << "基类:A\n";
	}
	void role() {
		cout << "啥也不干!\n";
	}
};
class B :public A
{
public:
	void Show() {
		cout << "派生类:B\n";
	}
	void role() {
		cout << "哈哈哈\n";
	}
};
class C :public A
{
public:
	void Show() {
		cout << "派生类:C\n";
	}
	void role() {
		cout << "啦啦啦\n";
	}
};

int main1(void)
{
	A a;
	B b;
	C c;
	/****************************************************
	强行转换,指鹿为马,非常危险:reinterpret_cast
	能用static_cast就不要用reinterpret_cast
	****************************************************/
	B* bp = reinterpret_cast<B*>(&c);
	bp->role();
	system("pause");
	return 0;
}

三、动态类型转换:dynamic_cast

#include<Windows.h>
#include<iostream>
using namespace std;
class A
{
public:
	virtual void Show() {
		cout << "基类:A\n";
	}
	void role() {
		cout << "啥也不干!\n";
	}
};
class B :public A
{
public:
	void Show() {
		cout << "派生类:B\n";
	}
	void role() {
		cout << "哈哈哈\n";
	}
};
class C :public A
{
public:
	void Show() {
		cout << "派生类:C\n";
	}
	void role() {
		cout << "啦啦啦\n";
	}
};

/*******************
动态类型转换:dynamic_cast
用于基类派生类之间的转换,基类必须有虚函数(多态)
*******************/
void test(A* a)
{
	a->Show();
	B* b = dynamic_cast<B*>(a);
	if (b) {
		b->role();
	}
	else {
		C* c = dynamic_cast<C*>(a);
		if (c) {
			c->role();
		}
		else {
			a->role();
		}
	}
}

void test(A& a)
{
	a.Show();
	try {
		B& b = dynamic_cast<B&>(a);
		b.role();
	}catch(bad_cast bc){
		//cout << bc.what() << endl;
		try {
			C& c = dynamic_cast<C&>(a);
			c.role();
		}
		catch (bad_cast bc) {
			//cout << bc.what() << endl;
			a.role();
		}
	}
}
int main3(void)
{
	A a;
	B b;
	C c;
	A* ap = &a;
	test(ap);
	ap = &b;
	test(ap);
	ap = &c;
	test(ap);
	cout << "----------------------\n";
	A& Ap = a;
	test(Ap);
	A& Bp = b;
	test(Bp);
	A& Cp = c;
	test(Cp);
	system("pause");
	return 0;
}

四、去掉const:const_cast

#include<Windows.h>
#include<iostream>
using namespace std;
/**********************
去掉指针,引用的const
**********************/
void test(const char* p)
{
	char* p1 = const_cast<char*>(p);
	const_cast<char*>(p)[1] = 'B';
	p1[0] = 'A';
	cout << p1 << endl;
}
int main2(void)
{
	char ch[] = "123456789";
	test(ch);

	//这里会引发异常,字符串常量在常量区,是不能修改的
	//在去掉const之前,要考虑指针指向的内存能否修改
	const char* p = "987654321";
	test(p);
	system("pause");
	return 0;
}