类型转换
13.3 C++类型转换运算符
4个C++类型转换运算符如下:
- static_cast
- dynamic_cast
- reinterpret_cast
- const_cast
使用语法:
destination_type result = cast_operator<destination_type> (object_to_cast);
13.3.1 使用static_cast
静态类型转换,对应于C语言中的隐式类型转换场景,可以转换基础数据类型,但是不能转换指针类型。该类型转换会在编译时进行类型检查。
#include <iostream>
using namespace std;
int main()
{
//1.转换基础数据类型
int a = 97;
char b = static_cast<char>(a);
cout << b << endl;
//2.转换指针类型
char str[] = "static_cast";
int* p;
//报错
//p = static_cast<int*>(str);
//错误 C2440 “static_cast” : 无法从“char[12]”转换为“int* ”
//错误(活动) E0171 类型转换无效
//解决方法,强制转换
p = reinterpret_cast<int*>(str); //相当于 p = (int*)str; //强制类型转换
cout << p << endl;
system("pause");
return 0;
}
13.3.2 使用reinterpret_cast
重新解释类型,可用于转换指针,但是不能转换基础数据类型。
#include <iostream>
using namespace std;
int main()
{
//1.转换指针类型
char str[] = "reinterpret_cast";
int* p;
p = reinterpret_cast<int*>(str); //相当于 p = (int*)str; //强制类型转换
cout << p << endl;
//2.转换基础数据类型
int a = 97;
char b;
//b = reinterpret_cast<char>(a);
//错误 C2440 “reinterpret_cast” : 无法从“int”转换为“char”
//错误(活动) E0171 类型转换无效
system("pause");
return 0;
}
13.3.3 使用dynamic_cast
动态类型转换,会进行动态类型检查,应用场景是在多态场景中(子类对象传给父类指针或引用),可以动态检查子类的类型,下面通过程序举例说明:
#include <iostream>
using namespace std;
class GetArea
{
public:
virtual double get_area(int a, int b) = 0;
};
class Square : public GetArea
{
public:
virtual double get_area(int a, int b)
{
return a * b;
}
void print_Square()
{
cout << "Is Square" << endl;
}
};
class Triangle : public GetArea
{
public:
virtual double get_area(int a, int b)
{
return a * b * 0.5;
}
void print_Triangle()
{
cout << "Is Triangle" << endl;
}
};
void print_area(int a, int b, GetArea* g)
{
cout << g->get_area(a, b) << endl;
//使用 dynamic_cast 进行动态类型检查
if (dynamic_cast<Square*>(g))
{
Square* s_temp = dynamic_cast<Square*>(g);
s_temp->print_Square();
}
else if (dynamic_cast<Triangle*>(g))
{
Triangle* t_temp = dynamic_cast<Triangle*>(g);
t_temp->print_Triangle();
}
else
{
cout << "Is Parent Class" << endl;
}
}
int main()
{
Square s;
Triangle t;
print_area(10, 10, &s);
print_area(10, 10, &t);
system("pause");
return 0;
}
运行程序并查看打印结果:
100
Is Square
50
Is Triangle
可以看到,通过dynamic_cast自动检查出了传入的子类究竟是矩形类还是三角形类。
在上面的基础上添加代码继续分析以下两种情况,一是使用其他转换对子类父类进行转换,二是将类转换为另一种类。具体分析如下:
class GetLen{};
void func_test()
{
GetArea* g = NULL;
Square* s = NULL;
g = s; //子类可以传给父类 //类型兼容性原则
//s = g; //不能直接将父类传给子类
//错误 C2440 “ = ”: 无法从“GetArea * ”转换为“Square * ”
//错误(活动) E0513 不能将 "GetArea *" 类型的值分配到 "Square *" 类型的实体
s = dynamic_cast<Square*>(g); //ok
s = static_cast<Square*>(g); //ok
s = reinterpret_cast<Square*>(g); //ok
GetLen* gl = NULL;; //其他类
//g = dynamic_cast<GetArea*>(gl);
//错误 C2683 “dynamic_cast” : “GetLen”不是多态类型
//错误(活动) E0698 运行时 dynamic_cast 的操作数必须包含多态类类型
//g = static_cast<GetArea*>(gl); //err
//错误 C2440 “static_cast” : 无法从“GetLen * ”转换为“GetArea* ”
//错误(活动) E0171 类型转换无效
g = reinterpret_cast<GetArea*>(gl); //ok
}
13.3.4 使用const_cast
应用场景是去除只读属性,但有一个前提是内存本身必须是可以修改的。
#include <iostream>
using namespace std;
void MyPrintStr1(const char* str) //const char* str,对实参设置只读属性,防止误修改
{
cout << str << endl;
}
void MyPrintStr2(const char* str)
{
cout << str << endl;
//str[0] = 'H'; //需要修改实参的场景,但const只读属性,无法修改内存
char* temp = NULL;
temp = const_cast<char*>(str);
temp[0] = 'H';
cout << str << endl;
}
int main()
{
//1.在堆栈上分配内存
char str1[] = "how to use const_cast";
MyPrintStr2(str1);
//2.使用常量区字符串(内存的常量区不可修改)
const char* pStr = "how to use const_cast";
MyPrintStr1(pStr);
//MyPrintStr2(pStr);
//引发了异常: 写入访问权限冲突。temp 是 0xDD9BF4。
//3.const 类型的字符串
const char str2[] = "how to use const_cast";
MyPrintStr2(str2);
system("pause");
return 0;
}
结果如下:
how to use const_cast
How to use const_cast
how to use const_cast
how to use const_cast
How to use const_cast
在测试函数中,我们分了三种情况,可以看出第1、3种情况可以正常通过const_cast去除只读属性,从而达到修改内存数据的目的,而第2种情况却出现了异常。这是因为,第1、3种情况虽然对字符串增加了只读属性,但是内存本身是在栈上分配的,而栈内存本身是可以修改的,所以运行成功。而第二种情况是使用了常量区(全局区)的内存,这段内存是不可修改的,所以即使我们使用const_cast去除了字符串的只读属性,也无法正常运行,这是因为内存本身不可修改的原因。所以,在使用const_cast的时候,首先要知道,const属性的变量是在哪分配的内存,该内存本身是否可以修改。
总结
一般C语言中能进行隐式类型转换的,都可以用 static_cast进行类型转换,指针不能进行隐式类型转换,也不能用static_cast转换;reinterpret_cast强制类型转换,重新解释类型,但不能用于C语言中能进行隐式类型转换的情况,也就是说static_cast和reinterpret_cast各司其职,不能混用;通过static_cast和reinterpret_cast,覆盖了C语言中的类型转换功能。dynamic_cast运行时类型识别,可用于区分子类。const_cast用于去除只读属性。
static_cast与dynamic_cast区别:static_cast是静态类型转换,在编译时进行类型检查;dynamic_cast是动态类型转换,在运行时进行类型检查。