类型转换使用建议
-
static_cast静态类型转换,编译的时c++编译器会做编译时的类型检查;隐式转换;
基本类型转换,父子类之间合理转换
-
若不同类型之间,进行强制类型转换,用reinterpret_cast<>() 进行重新解释
-
建 议:
C语言中 能隐式类型转换的,在c++中可用 static_cast<>()进行类型转换。因C++编译器在编译检查一般都能通过;C语言中不能隐式类型转换的,在c++中可以用 reinterpret_cast<>() 进行强制类型解释。
-
总结:static_cast<>()和reinterpret_cast<>() 基本上把C语言中的 强制类型转换给覆盖,注意reinterpret_cast<>()很难保证移植性。
-
dynamic_cast<>(),动态类型转换,安全的虚基类和子类之间转换;运行时类型检查
-
const_cast<>(),去除变量的只读属性
最后的忠告:程序员必须清楚的知道: 要转的变量,类型转换前是什么类型,类型转换 后是什么类型,转换后有什么后果。
C++大牛建议:一般情况下,不建议进行类型转换;避免进行类型转换。
C++的类型转换
-
旧式转型 C风格的强制类型:
TYPE b = (TYPE) a
例如:
int i = 48;
char c = (char) i; //强制转换
-
新式转型C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用。
格式:
TYPE b = 类型操作符<TYPE> ( a )
类型操作符= static_cast | reinterpreter_cast | dynamic_cast | const_cast
static_cast
静态类型转换(斯文的劝导,温柔的转换)。如int转换成char
主要用法:
- 用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换。上行指针或引用(派生类到基类)转换安全,下行不安全
- 用于基本数据类型之间的转换,如把int转换成char,把int转换成enum。这种转换的安全性也要开发人员来保证。
- 把空指针转换成目标类型的空指针。
- 把任何类型的表达式转换成void类型。
#include <iostream>
#include <Windows.h>
using namespace std;
class Animal {public: virtual void cry() = 0;};
class Dog : public Animal {public: void cry() {cout << "汪汪汪" << endl;}};
class Cat : public Animal {public: void cry() {cout << "喵喵喵" << endl;}};
int main(void){
//方式一, 父类与子类之间的转换
Dog *d1 = new Dog();
Animal* a1 = static_cast<Animal*>(d1); //子类指针转型为父类
d1 = static_cast<Dog*>(a1); //父类指针转换为子类指针
Cat* c1 = static_cast<Cat*>(a1);//不安全, 因为此时父类指向了Dog类
c1->cry(); //输出的是Dog类的 "汪汪汪"
Dog d2;
Animal& a2 = static_cast<Animal&>(d2); //子类引用转型到父类
//方式二, 普通类型转换
int k = 97;
char c = static_cast<char>(k);
cout << c << endl;
//方式三, 空指针转换为目标类型的空指针
int* p1 = static_cast<int*>(NULL);
char* cp = static_cast<char*>(NULL);
//方式四, 把任何类型的表达式,转换为void类型
int* p = new int[10];
void* vp = static_cast<void*>(p);
system("pause");
return 0;
}
reinterpret_cast
-
重新解释类型(挂羊头,卖狗肉) 不同类型间的互转,数值与指针间的互转
-
用法: TYPE b = reinterpret_cast ( a )
TYPE必须是一个指针、引用、算术类型、函数指针.
-
忠告:滥用 reinterpret_cast 运算符可能很容易带来风险。 除非所需转换本身是低级别的,否则应使用其他强制转换运算符之一。
#include <iostream>
#include <Windows.h>
using namespace std;
class Animal {public: void cry() {cout << "动物叫" << endl;}};
class Dog : public Animal {public: void cry() {cout << "汪汪汪" << endl;}};
class Cat : public Animal {public: void cry() {cout << "喵喵喵" << endl;}};
int main(void){
//用法一, 普通类型之间的转换
int* p = reinterpret_cast<int*>(0x888888);
int val = reinterpret_cast<int>(p);
//用法二, 不同指针类型之间的转换, 和引用之间的转换
Dog d1;
Animal* a1 = reinterpret_cast<Animal*>(&d1);
Dog* d2 = reinterpret_cast<Dog*>(a1);
Dog* d3 = static_cast<Dog*>(a1); //能使用static_cast, 尽量优先使用
//Cat* c1 = static_cast<Cat*>(&d1); //NO! 不同类型之间的转换, 不能使用static_cast
Cat* c2 = reinterpret_cast<Cat*>(&d1); //不同类型之间的转换, 可以使用reinterpret_cast
system("pause");
return 0;
}
dynamic_cast
- 动态类型转换
- 将一个基类对象指针cast到继承类指针,dynamic_cast 会根据基类指针是否真正指向继承类指针来做相应处理。失败返回null,成功返回正常cast后的对象指针;
- 将一个基类对象引用cast 继承类对象,dynamic_cast 会根据基类对象是否真正属于继承类来做相应处理。失败抛出异常bad_cast
- 注意:dynamic_cast在将父类cast到子类时,父类必须要有虚函数一起使用
#include <iostream>
#include <Windows.h>
using namespace std;
class Animal {
public:
virtual void cry() = 0;
virtual void play() = 0;
};
class Dog : public Animal {
public:
void cry() {cout << "狗: 汪汪汪" << endl;}
void play() { cout << "打个滚" << endl; }
};
class Cat : public Animal {
public:
void cry() {cout << "猫: 喵喵喵" << endl;}
void play() { cout << "爬上树" << endl; }
};
void animalPlay(Animal* ani) {
ani->cry();
Dog* dog = dynamic_cast<Dog*>(ani);
if (dog) {
dog->play();
} else {
cout << "不是狗, 别骗我!" << endl;
}
Cat* cat = dynamic_cast<Cat*>(ani);
if (cat) {
cat->play();
} else {
cout << "不是猫, 别骗我!" << endl;
}
}
void animalPlay2(Animal& ani) {
ani.cry();
try {
Dog& dog = dynamic_cast<Dog&>(ani);
dog.play();
} catch (std::bad_cast) {
cout << "不是狗, 应该是下面的猫" << endl;
}
try {
Cat& cat = dynamic_cast<Cat&>(ani);
cat.play();
} catch (std::bad_cast) {
cout << "不是猫, 应该是上面的狗" << endl;
}
}
int main(void){
Dog *dog = new Dog();
Animal* a1 = dog;
//animalPlay(a1);
Cat* cat = new Cat();
a1 = cat;
//animalPlay(a1);
Dog dog2;
animalPlay2(dog2);
Cat cat2;
animalPlay2(cat2);
system("pause");
return 0;
}
const_cast
去const属性。(仅针对于指针和引用)
#include <iostream>
#include <Windows.h>
using namespace std;
void test(const char* p) {
//去const属性
char* p1 = const_cast<char*>(p);
p1[0] = 'A';
}
int main(void){
char c1[] = "12345678";
test(c1);
cout << c1 << endl; //输出 "A2345678"
system("pause");
return 0;
}