更严格的类型转换
类型转换(cast)是将一种数据类型转换成另一种数据类型。例如,如果将一个整型值赋给一个浮点类型的变量,编译器会暗地里将其转换成浮点类型。
转换是非常有用的,但是它也会带来一些问题,比如在转换指针时,我们很可能将其转换成一个比它更大的类型,但这可能会破坏其他的数据。
应该小心类型转换,因为转换也就相当于对编译器说:忘记类型检查,把它看做其他的类型。一般情况下,尽量少的去使用类型转换,除非用来解决非常特殊的问题。
无论什么原因,任何一个程序如果使用很多类型转换都值得怀疑。
标准C++提供了一个显式的转换的语法,来替代旧的C风格的类型转换。使用C风格的强制转换可以把想要的任何东西转换成我们需要的类型。那为什么还需要一个新的C++类型的强制转换呢?
在C++中不同类型的变量一般是不能直接赋值的,需要相应的强转。
#include <stdio.h>
#include <stdlib.h>
//定义一个枚举列表 并初始化给该类型的枚举变量color
typedef enum COLOR { GREEN, RED, YELLOW } color;
int main(int argc, char *argv[]) {
color mycolor = GREEN;
mycolor = 10;
printf("mycolor:%d\n", mycolor);
char* p = malloc(10);
return 0;
}
C语言环境运行程序输出:
mycolor:10
C++环境:
以上C代码C编译器编译可通过,C++编译器无法编译通过。
Tips
新类型的强制转换可以提供更好的控制强制转换过程的机制,允许控制各种不同种类的强制转换。C++风格的强制转换其他的好处是,它们能更清晰的表明它们要干什么。程序员只要扫一眼这样的代码,就能立即知道一个强制转换的目的。
C++语言级别提供了四种类型转换
static_cast
:提供编译器认为安全的类型转换dynamic_cast
:主要用在继承结构中,可以支持RTTI类型识别的上下文转换const_cast
:去掉常量属性的类型转换reinterpret_cast
:类似于C风格的强制类型转换,是最不安全的 C++中一般不要使用
static_cast 静态转换
语法为:static_cast<目标类型>(源变量/源对象)
用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换。
进行上行转换(把派生类的指针或引用转换成基类表示)是安全的;
进行下行转换(把基类指针或引用转换成派生类表示)时,由于没有动态类型检查,所以是不安全的。
用于基本数据类型之间的转换,如把int转换成char,把char转换成int。这种转换的安全性也要开发人员来保证。
#include <cstddef>
class Animal {};
class Dog : public Animal {};
class Other {};
// 1、基础数据类型转换
void test01() {
char a = 'a';
double b = static_cast<double>(a);
}
// 2、继承关系指针互相转换
void test02() {
Animal *animal01 = NULL;
Dog *dog01 = NULL;
// 2.1、子类指针转成父类指针,安全
Animal *animal02 = static_cast<Animal *>(dog01);
// 2.2、父类指针转成子类指针,不安全
Dog *dog02 = static_cast<Dog *>(animal01);
}
// 3、继承关系引用相互转换
void test03() {
Animal ani_ref;
Dog dog_ref;
Animal &animal01 = ani_ref;
Dog &dog01 = dog_ref;
// 3.1、子类指针转成父类指针,安全
Animal &animal02 = static_cast<Animal &>(dog01);
// 3.2、父类指针转成子类指针,不安全
Dog &dog02 = static_cast<Dog &>(animal01);
}
//无继承关系指针转换——无法进行转换
void test04() {
Animal *animal01 = NULL;
Other *other01 = NULL;
//转换失败
//Animal* animal02 = static_cast<Animal*>(other01);
}
dynamic_cast 动态转换
语法为:dynamic_cast<目标类型>(源变量/源对象)
- ;
在类层次间进行上行转换时,
dynamic_cast
和static_cast
的效果是一样的;;
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);
}
普通的父转子是不安全的,不允许使用dynamic_cast
进行转换,但是如果发生了多态,转换是被允许的。
⭐️注意
static_cast
可以看作是编译时期的类型转换dynamic_cast
可以看作是运行时期的类型转换,支持RTTI类型信息识别
下面通过实例演示二者的区别,假定有如下程序代码
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() = 0;
};
class Derive1 : public Base {
public:
void show() { cout << "Derive1::show()" << endl; }
};
class Derive2 : public Base {
public:
void show() { cout << "Derive2::show()" << endl; }
};
void show(Base *p) {
p->show();
}
int main(int argc, char *argv[]) {
Base *pb1 = new Derive1();
Base *pb2 = new Derive2();
show(pb1);
show(pb2);
delete pb1;
delete pb2;
return 0;
}
代码经过编译后运行结果如下
$ g++ 001.cpp
$ ./a.out
Derive1::show()
Derive2::show()
但是,之后的某一天需求改变了。要求在show函数中识别如果传入的是derive2类型的对象就调用derive2中新实现的功能,传入其他对象的话保持不变
首先按照要求对代码进行以下实现
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() = 0;
};
class Derive1 : public Base {
public:
void show() { cout << "Derive1::show()" << endl; }
};
class Derive2 : public Base {
public:
void show() { cout << "Derive2::show()" << endl; }
void newShow() { cout << "Derive2::newShow()" << endl; }
};
void show(Base *p) {
Derive2 *pb = dynamic_cast<Derive2 *>(p);
//Derive2 *pb = static_cast<Derive2 *>(p);
if (pb != nullptr) {
pb->newShow();
} else {
p->show();
}
}
int main(int argc, char *argv[]) {
Base *pb1 = new Derive1();
Base *pb2 = new Derive2();
show(pb1);
show(pb2);
delete pb1;
delete pb2;
return 0;
}
先用dynamic_cast
进行实现,发现运行结果与需求相符
$ g++ 001.cpp
$ ./a.out
Derive1::show()
Derive2::newShow()
然后把dynamic_cast
更换为static_cast
,发现程序运行结果与我们预期的并不一致!
$ g++ 001.cpp
$ ./a.out
Derive2::newShow()
Derive2::newShow()
导致这种结果的原因就是两者的机制不同,一个支持RTTI类型信息识别,一个不支持。
const_cast 常量转换
语法为: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);
}
reinterpret_cast 重新解释转换
语法为:reinterpret_cast<目标类型>(源变量/源对象)