类型转换const_cast、reinterpret_cast、dynamic_cast、static_cast
- const_cast
- reinterpret_cast
- dynamic_cast
- static_cast
const_cast
被const修饰的函数可以被访问,但是不能被修改成员变量
const_cast可以去掉const
#include <iostream>
using namespace std;
class FHello
{
public:
FHello();
void Init();/*const*/
private:
int a;
int b;
float c;
};
FHello::FHello()
{
a = 0;
b = 10;
c = 20.f;
}
void FHello::Init()/*const*/
{
auto Hello_a = [&]()
{
cout << a << endl;
};
auto Hello_b = [&]()->bool
{
cout << b << endl;
return true;
};
Hello_a();
if (bool bHello = Hello_b())
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
}
int main()
{
const FHello* HelloTest = new FHello();
//HelloTest->Init();因为HelloTest被const修饰了而上面代码中没有用const修饰
//用const_cast去掉const
FHello* Test = const_cast<FHello*>(HelloTest);
Test->Init();
//编译出错会返回NULL
//c风格的强转,是万能强转//容易出问题,万一转换失败了变成野指针了也不知道,少用
FHello* NewTest = (FHello*)HelloTest;
NewTest->Init();
delete HelloTest;
return 0;
}
reinterpret_cast
把指针转换成int,也可以把int转成指针,也可以指针转换成指针
#include <iostream>
using namespace std;
class FHello
{
public:
FHello();
void Init();/*const*/
private:
int a;
int b;
float c;
};
FHello::FHello()
{
a = 0;
b = 10;
c = 20.f;
}
void FHello::Init()/*const*/
{
auto Hello_a = [&]()
{
cout << a << endl;
};
auto Hello_b = [&]()->bool
{
cout << b << endl;
return true;
};
Hello_a();
if (bool bHello = Hello_b())
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
}
int main()
{
//const_cast
const FHello* HelloTest = new FHello();
FHello* Test = const_cast<FHello*>(HelloTest);
Test->Init();
FHello* NewTest = (FHello*)HelloTest;
NewTest->Init();
//reinterpret_cast
//reinterpret_cast,先转换成int,再转换成指针
int a = reinterpret_cast<int>(NewTest);
FHello*b = reinterpret_cast<FHello*>(a);
b->Init();
//也可以转换成int*指针,也可以转换回来
int* a2 = reinterpret_cast<int*>(NewTest);
FHello* b2 = reinterpret_cast<FHello*>(a2);
b2->Init();
//可以转换成float*指针,但是不可以用float类型
float* a3 = reinterpret_cast<float*>(NewTest);
FHello* b3 = reinterpret_cast<FHello*>(a3);
b3->Init();
delete HelloTest;
return 0;
}
dynamic_cast
动态的转化:可以向上转化也可以向下转化
向上转化和向下转化的意思是在数层的继承关系中,可以向父类或派生类进行转化
向下转换时会进行类型检测,如果类型无法转换回返回NULL。是一个比较安全的转换
接口(父类)必须有虚函数,否则会报错
#include <iostream>
using namespace std;
class FHello
{
public:
FHello();
virtual ~FHello();
void Init();/*const*/
private:
int a;
int b;
float c;
};
FHello::FHello()
{
a = 0;
b = 10;
c = 20.f;
}
FHello::~FHello() {}
void FHello::Init()/*const*/
{
auto Hello_a = [&]()
{
cout << a << endl;
};
auto Hello_b = [&]()->bool
{
cout << b << endl;
return true;
};
Hello_a();
if (bool bHello = Hello_b())
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
}
class FHello1:public FHello
{
public:
void Hello1temp() {}
private:
int a;
};
class FHello2 :public FHello1
{
};
int main()
{
//const_cast
const FHello* HelloTest = new FHello();
FHello* Test = const_cast<FHello*>(HelloTest);
Test->Init();
FHello* NewTest = (FHello*)HelloTest;
NewTest->Init();
//reinterpret_cast
int a = reinterpret_cast<int>(NewTest);
FHello*b = reinterpret_cast<FHello*>(a);
b->Init();
int* a2 = reinterpret_cast<int*>(NewTest);
FHello* b2 = reinterpret_cast<FHello*>(a2);
b2->Init();
float* a3 = reinterpret_cast<float*>(NewTest);
FHello* b3 = reinterpret_cast<FHello*>(a3);
b3->Init();
//dynamic_cast
FHello* h1 = new FHello1();//这一步也是向上转换
//向下转换:
FHello1* A = dynamic_cast<FHello1*>(h1);
A->Hello1temp();
//向上转换,用的少,因为有更简单的方法,如上
FHello* B = dynamic_cast<FHello*>(A);
B->Init();
FHello* B2 = A;//进行隐式转换
FHello2* FHA2 = dynamic_cast<FHello2*>(h1);//转换失败,输出NULL
if (FHA2)
{
}
delete HelloTest;
delete h1;
return 0;
}
static_cast
可以向上转化也可以向下转化
向下转换时不安全
static_cast不可以转换const、volitale、_unaligned
static_cast和dynamic_cast相比static_cast可以没有虚函数,而dynamic_cast必须有虚函数
格式
FHello* A=static_cast<FHello*>(B);