类型转换(cast)是将一种数据类型转换成另一种数据类型。例如,如果将一个整型值赋给一个浮点类型的变量,编译器会暗地里将其转换成浮点类型。
转换是非常有用的,但是它也会带来一些问题,比如在转换指针时,我们很可能将其转换成一个比它更大的类型,但这可能会破坏其他的数据。
应该小心类型转换,因为转换也就相当于对编译器说:忘记类型检查,把它看做其他的类型。
一般情况下,尽量少的去使用类型转换,除非用来解决非常特殊的问题。
无论什么原因,任何一个程序如果使用很多类型转换都值得怀疑. |
标准c++提供了一个显示的转换的语法,来替代旧的C风格的类型转换。
使用C风格的强制转换可以把想要的任何东西转换成我们需要的类型。那为什么还需要一个新的C++类型的强制转换呢?
新类型的强制转换可以提供更好的控制强制转换过程,允许控制各种不同种类的强制转换。C++风格的强制转换其他的好处是,它们能更清晰的表明它们要干什么。程序员只要扫一眼这样的代码,就能立即知道一个强制转换的目的。
6.2.1 静态转换(static_cast)
1.用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换。
- 进行上行转换(把派生类的指针或引用转换成基类表示)是安全的;
- 进行下行转换(把基类指针或引用转换成派生类表示)时,由于没有动态类型检查,所以是不安全的。
2.用于基本数据类型之间的转换,如把int转换成char,把char转换成int。这种转换的安全性也要开发人员来保证。
class Animal {};
class Dog :public Animal {};
class Other {};
//基础数据类型转换
void test01()
{
char a = 'a';
double b = static_cast<double>(a);
}
//继承关系指针互相转换
void test02()
{
//继承关系指针转换
Animal* animal01 = NULL;
Dog* dog01 = NULL;
//子类指针转换成父类指针,安全
Animal* animal02 = static_cast<Animal*>(dog01);
//父类指针转换成子类指针,不安全
Dog* dog02 = static_cast<Dog*>(animal01);
}
//继承关系引用相互转换
void test03()
{
Animal animal_ref;
Dog dog_ref;
//继承关系指针转换
Animal& animal01 = animal_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* animal01 = static_cast<Animal*>(other01);
}
int main()
{
test01();
test02();
test03();
test04();
return 0;
}
6.2.2 动态转换(dynamic_cast)
- dynamic_cast主要用于类层次间的上行转换和下行转换;
- 在类层次间进行上行转换时,dynamic_cast和static_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 Animal01
{
public:
void ShowName01()
{
cout << "普通多态" << endl;
}
};
class Dog01 :public Animal01
{
void Print()
{
cout << "I am a dog" << endl;
}
};
class Other
{
public:
void Print()
{
cout << "其他类" << endl;
}
};
//普通类型转换
void test01()
{
//不支持基础数据的类型转换
int a = 10;
//double a = dynamic_cast<double>(a);
}
//继承关系指针
void test02()
{
Animal* animal = NULL;
Dog* dog = new Dog;
Animal01* animal01 = NULL;
Dog01* dog01 = new Dog01;
//子类指针转换成父类指针,可以
Animal* animal02 = dynamic_cast<Animal*>(dog);
animal02->ShowName();
//父类转换成子类指针,多态继承可以,普通继承不可以
Dog* dog02 = dynamic_cast<Dog*>(animal);
//Dog01* dog3 = dynamic_cast<Dog01*>(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* animal01 = dynamic_cast<Animal*>(other);
}
int main()
{
test01();
test02();
test03();
test04();
return 0;
}
输出结果
I am a dog
I am a dog
6.2.3 常量转换(const_cast)
该运算符用来修改类型的const属性。
1.常量指针被转化成非常量指针,并且仍然指向原来的对象;
2.常量引用被转换成非常量引用,并且仍然指向原来的对象;
注意:不能直接对非指针和非引用的变量使用const_cast操作符去直接移除它的const. |
void test01()
{
//1.常量指针转换成非常量指针
const int* p = NULL;
int* np = const_cast<int*>(p);
//2.非常量指针转换成常量指针
int* pp = NULL;
const int* npp = const_cast<const int*>(pp);
//3.不能对非指针或非引用进行转换
const int a = 10;
//int b = const_cast<int>(a);
}
void test02()
{
//4.非常量引用转换成常量引用
int num = 10;
int& retNum = num;
const int& refNum2 = const_cast<const int&>(retNum);
//5.常量引用转换成非常量引用
int num01 = 10;
const int& retNum01 = num;
int& refNum02 = const_cast<int&>(retNum01);
}
int main()
{
test01();
test02();
return 0;
}
6.2.3 重新解释转换(reinterpret_cast)
这是最不安全的一种转换机制,最有可能出问题。
主要用于将一种数据类型从一种类型转换为另一种类型。它可以将一个指针转换成一个整数,也可以将一个整数转换成一个指针。实际操作中几乎不用,所以了解即可。