类型转换(cast)是将一种数据类型转换成另一种数据类型。例如,如果将一个整型值赋给一个浮点类型的变量,编译器会暗地里将其转换成浮点类型。
转换是非常有用的,但是它也会带来一些问题,比如在转换指针时,我们很可能将其转换成一个比它更大的类型,但这可能会破坏其他的数据。
应该小心类型转换,因为转换也就相当于对编译器说:忘记类型检查,把它看做其他的类型。
一般情况下,尽量少的去使用类型转换,除非用来解决非常特殊的问题。
无论什么原因,任何一个程序如果使用很多类型转换都值得怀疑
标准c++提供了一个显示的转换的语法,来替代旧的C风格的类型转换。
使用C风格的强制转换可以把想要的任何东西转换成我们需要的类型。那为什么还需要一个新的C++类型的强制转换呢?
新类型的强制转换可以提供更好的控制强制转换过程,允许控制各种不同种类的强制转换。C++风格的强制转换其他的好处是,它们能更清晰的表明它们要干什么。程序员只要扫一眼这样的代码,就能立即知道一个强制转换的目的。
1 静态转换(static_cast)
- 用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换。
- 进行上行转换(把派生类的指针或引用转换成基类表示)是安全的;
- 进行下行转换(把基类指针或引用转换成派生类表示)时,由于没有动态类型检查,所以是不安全的。
- 用于基本数据类型之间的转换,如把int转换成char,把char转换成int。这种转换的安全性也要开发人员来保证。 ```cpp class Animal{}; class Dog : public Animal{}; class Other{};
//基础数据类型转换
void test01(){
char a = ‘a’;
double b = static_cast
//继承关系指针互相转换 void test02(){ //继承关系指针转换 Animal animal01 = NULL; Dog dog01 = NULL; //子类指针转成父类指针,安全 Animal animal02 = static_cast<Animal>(dog01); //父类指针转成子类指针,不安全 Dog dog02 = static_cast<Dog>(animal01); }
//继承关系引用相互转换 void test03(){
Animal ani_ref;
Dog dog_ref;
//继承关系指针转换
Animal& animal01 = ani_ref;
Dog& dog01 = dog_ref;
//子类指针转成父类指针,安全
Animal& animal02 = static_cast<Animal&>(dog01);
//父类指针转成子类指针,不安全
Dog& dog02 = static_cast<Dog&>(animal01);
}
//无继承关系指针转换 void test04(){
Animal* animal01 = NULL;
Other* other01 = NULL;
//转换失败
//Animal* animal02 = static_cast<Animal*>(other01);
}
<a name="O6a6d"></a>
## 2 动态转换(dynamic_cast)
1. `dynamic_cast`主要用于类层次间的上行转换和下行转换;
1. 在类层次间进行上行转换时,dynamic_cast和static_cast的效果是一样的;
1. 在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全;
```cpp
class Animal {
public:
virtual void ShowName() = 0;
};
class Dog : public Animal{
virtual void ShowName(){
cout << "I am a dog!" << endl;
}
};
class Other {
public:
void PrintSomething(){
cout << "我是其他类!" << endl;
}
};
//普通类型转换
void test01(){
//不支持基础数据类型
int a = 10;
//double a = dynamic_cast<double>(a);
}
//继承关系指针
void test02(){
Animal* animal01 = NULL;
Dog* dog01 = new Dog;
//子类指针转换成父类指针 可以
Animal* animal02 = dynamic_cast<Animal*>(dog01);
animal02->ShowName();
//父类指针转换成子类指针 不可以
//Dog* dog02 = dynamic_cast<Dog*>(animal01);
}
//继承关系引用
void test03(){
Dog dog_ref;
Dog& dog01 = dog_ref;
//子类引用转换成父类引用 可以
Animal& animal02 = dynamic_cast<Animal&>(dog01);
animal02.ShowName();
}
//无继承关系指针转换
void test04(){
Animal* animal01 = NULL;
Other* other = NULL;
//不可以
//Animal* animal02 = dynamic_cast<Animal*>(other);
}
3 常量转换(const_cast)
该运算符用来修改指针和引用类型的const属性
- 常量指针被转化成非常量指针,并且仍然指向原来的对象;
- 常量引用被转换成非常量引用,并且仍然指向原来的对象;
注意:不能直接对非指针和非引用的变量使用const_cast操作符去直接移除它的const
//常量指针转换成非常量指针
void test01(){
const int* p = NULL;
int* np = const_cast<int*>(p);
int* pp = NULL;
const int* npp = const_cast<const int*>(pp);
const int a = 10; //不能对非指针或非引用进行转换
//int b = const_cast<int>(a); }
//常量引用转换成非常量引用
void test02(){
int num = 10;
int & refNum = num;
const int& refNum2 = const_cast<const int&>(refNum);
}
4 重新解释转换(reinterpret_cast)
这是最不安全的一种转换机制,最有可能出问题。
主要用于将一种数据类型从一种类型转换为另一种类型。它可以将一个指针转换成一个整数,也可以将一个整数转换成一个指针.
5 参考代码汇总
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//1、静态类型转换 static_cast
void test01()
{
//允许内置数据类型之间转换
char a = 'a';
double d = static_cast<double>(a);
cout << d << endl;
}
class Base{ virtual void func(){} };
class Son :public Base{ virtual void func(){} };
class Other{};
void test02()
{
Base * base = NULL;
Son * son = NULL;
//语法: static_cast<目标类型>(原对象)
//父子之间的指针或者引用可以转换
//将base 转为 Son * 父转子 向下类型转换 不安全
Son * son2 = static_cast<Son *> (base);
//son 转为 Base* 子转父 向上类型转换 安全
Base * base2 = static_cast<Base*>(son);
//base 转为Other*
//Other * other = static_cast<Other*>(base); 转换无效
}
//动态类型转换 dynamic_cast
void test03()
{
//不允许内置数据类型之间转换
//char c = 'c';
//double d = dynamic_cast<double>(c);
}
void test04()
{
Base * base = new Son;
Son * son = NULL;
//将base 转为 Son * 父转子 不安全 如果发生了多态,那么转换总是安全的
Son * son2 = dynamic_cast<Son*>(base);
//son 转为 Base* 子转父 安全
Base * base2 = dynamic_cast<Base*>(son);
//base 转 Other*
//Other* other = dynamic_cast<Other*>(base); //无法转换
}
//常量转换 const_cast
void test05()
{
//不可以将非指针或非引用做const_cast转换
const int * p = NULL;
int * pp = const_cast<int*>(p);
const int * ppp = const_cast<const int *>(pp);
//const int a = 10;
//int b = const_cast<int>(a);
int num = 10;
int &numRef = num;
const int& num2 = const_cast<const int&>(numRef);
}
//重新解释转换 reinterpret_cast 最不安全一种转换,不建议使用
void test06()
{
int a = 10;
int * p = reinterpret_cast<int*>(a);
Base * base = NULL;
//base 转 Other *
Other * other = reinterpret_cast<Other *>(base);
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
/* 输出结果
97
*/