类型转换名称和语法
C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:
TYPE b = (TYPE)a
C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用。
static_cast 静态类型转换。如int转换成char
reinterpreter_cast 重新解释类型
dynamic_cast 命名上理解是动态类型转换。如子类和父类之间的多态类型转换。
const_cast, 字面上理解就是去const属性。
4种类型转换的格式:
TYPE B = static_cast<TYPE> (a)
demo 1
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
double pi = 3.1415926;
int num1 = (int)pi; // C类型转换
int num2 = static_cast<int>(pi); // 静态类型转换,编译的时候C++编译器会做类型检查
int num3 = pi; // C语言中隐式类型转换的地方,均可使用static_cast<>()进行类型转换
char *p1 = "hello";
int *p2 = NULL;
//p2 = static_cast<int *>(pi); // 使用static_cast,编译时编译器会报错
// 会做类型检查,若有错误,提示错误
p2 = reinterpret_cast<int *>(p1); // 若不同类型之间,进行强制类型转换,用reinterpret_cast<>()进行重新解释
cout << "p1: " << p1 << endl; // %s
cout << "p2: " << p2 << endl; // %d
/*
p1: hello
p2: 0127CA54
*/
return 0;
}
总结:
1)static_cast<>() 静态类型转换,编译的时c++编译器会做类型检查;
基本类型能转换 但是不能转换指针类型。
2)若不同类型之间,进行强制类型转换,用reinterpret_cast<>() 进行重新解释;
3)一般性结论:
C语言中 能隐式类型转换的,在c++中可用 static_cast<>()进行类型转换。因C++编译器在编译检查一般都能通过;
C语言中不能隐式类型转换的,在c++中可以用 reinterpret_cast<>() 进行强行类型。
解释。总结:static_cast<>()和reinterpret_cast<>() 基本上把C语言中的 强制类型转换给覆盖。
reinterpret_cast<>()很难保证移植性。
demo 2 - dynamic_cast<>(),动态类型转换,安全的基类和子类之间转换;运行时类型检查
#include <iostream>
#include <cstdio>
using namespace std;
class Tree {};
class Animal
{
virtual void cry() = 0;
};
class Dog : public Animal
{
public:
virtual void cry()
{
cout << "bowwow~" << endl;
}
void doHome()
{
cout << "look after the house!" << endl;
}
};
class Cat : public Animal
{
public:
virtual void cry()
{
cout << "meow" << endl;
}
void doThing()
{
cout << "catch a Mouse" << endl;
}
};
void playObj(Animal *base)
{
base->cry(); // 1有继承,2虚函数重载,3父类指针,指向子类对象。
// 会发生多态
// 识别子类对象
// dynamic_cast<>()运行时类型识别
Dog *pDog = dynamic_cast<Dog *>(base);
if (pDog != NULL) {
pDog->doHome();
}
Cat *pCat = dynamic_cast<Cat *>(base);
if (pCat != NULL) {
pCat->doThing();
}
}
int main()
{
Dog d1;
Cat c1;
Animal *pBase = NULL;
pBase = &d1;
pBase = static_cast<Animal *>(&d1); // 让C++编译器在编译的时候进行类型检查
// 强制类型转换
pBase = reinterpret_cast<Animal *>(&d1);
{
Tree t1;
//pBase = static_cast<Animal *>(&t1); // C++编译器做类型检查,报错
pBase = reinterpret_cast<Animal *>(&t1); // reinterpret_cast重新解释,这里有强制类型转换的感觉
}
playObj(&d1);
playObj(&c1);
return 0;
}
demo 3 - const_cast<>(),去除变量的只读属性
#include <iostream>
#include <cstdio>
using namespace std;
// const char *p的const修饰,让p指向的内存空间变成只读属性
void printBuf(const char *p)
{
//p[0] = 'Z'; // 报错
char *p1 = NULL;
// 程序员需要清楚的知道变量转换之前是什么类型,转换之后是什么类型
// const char * ==> char * 把只读属性去掉了
p1 = const_cast<char *>(p);
p1[0] = 'Z'; // 通过p1修改了内存空间
cout << p << endl;
}
int main()
{
char buf[] = "aaaaaaaasssssssssdddddddd";
printBuf(buf);
// 注意,要确保p所指向的内存空间确实能修改,如果不能修改,会带来灾难性后果
char *myp = "aaaaaadfdafaf"; // myp指向一块常量空间,这里是无论如何都不能被修改的
//printBuf(myp); // 运行直接奔溃
return 0;
}
总结
结论1:程序员要清除的知道: 要转的变量,类型转换前是什么类型,类型转换后是什么类型。转换后有什么后果。
结论2:一般情况下,不建议进行类型转换;避免进行类型转换。