1. static_cast:用于基本数据类型之间的转换,以及普通工具之间的转换,还可以用来将子类指针或引用转换为父类指针或引用。
2. dynamic_cast:用于将子类指针或引用转换为父类指针或引用,须要在运行时检讨类型安全性,并且只能用于具有多态性的类,如果转换失落败则返回空指针或抛出非常。
3. reinterpret_cast:用于进行指针之间的逼迫类型转换,可以将任何类型的指针转换为任何其他类型的指针,但是可能会导致不可预期的行为,该当慎重利用。

4. const_cast:用于去除常量性,可以将常量类型转换为非常量类型,或者将非常量类型转换为常量类型。
下面对这四种逼迫类型转换函数进行更详细的先容。
static_caststatic_cast是最常用的类型转换函数之一,用于在编译时进行静态类型检讨,因此比较安全。可以利用static_cast函数将一种基本数据类型转换为另一种,也可以将普通工具之间进行转换,还可以用来将子类指针或引用转换为父类指针或引用。
对付基本类型之间的转换,static_cast的语法格式如下:
static_cast <type-id> ( expression )
个中,type_id是转换后的数据类型,expression是要转换的表达式。
// 1.基本类型变量类型转换int n = 10;double x = static_cast<double>(n);// 2.类变量类型转换class Base {public: virtual ~Base() {} virtual void func() = 0;};class Derived : public Base {public: void func() override { cout << "Derived::func()" << endl; }};Base p = new Derived;Derived q = static_cast<Derived>(p);
在上述示例中,p是指向Derived工具的指针,通过static_cast将其转换为Derived类型的指针,这样就可以调用Derived类中定义的成员函数了。
小结:
static_cast 运算符可以将整数值显式转换为列举类型。 如果整型值不在列举值的范围内,天生的列举值是不愿定的。static_cast 运算符将空指针值转换为目标类型的空指针值。任何表达式都可以通过 static_cast 运算符显式转换为 void 类型。 目标 void 类型可以选择性地包含 const、volatile 或 __unaligned 特性。static_cast 运算符无法转换掉 const、 volatile或 __unaligned 特性。dynamic_castdynamic_cast用于将子类指针或引用转换为父类指针或引用,须要在运行时进行检讨,用于多态类型下的转换,可以检讨类型安全性。如果转换成功,返回转换后的工具指针或引用,否则返回空指针或抛出非常。
dynamic_cast的语法格式如下:
dynamic_cast < type-id > ( expression )
个中,type_id是要转换的工具类型,expression是要转换的工具的指针或引用。例如:
class Base {public:virtual ~Base() {}};class Derived : public Base {public:void func() { cout << "Derived::func()" << endl; }};Base p = new Derived;Derived q = dynamic_cast<Derived>(p);if (q != nullptr) {q->func();} else {cout << "Cast failed." << endl;}
在上述示例中,p是指向Derived工具的指针,通过dynamic_cast将其转换为Derived类型的指针。如果转换成功,q将指向Derived工具,可以调用Derived类中定义的成员函数func。否则,q将为nullptr,输出提示信息。
须要把稳的是,dynamic_cast只能用于具有多态性的类,也便是说,只有在子类中定义了虚函数,并且父类将这些虚函数声明为virtual,才能利用dynamic_cast进行类型转换。
此外,dynamic_cast还可以用来判断一个指针或引用是否能够转换为另一种类型。如果可以转换,则返回转换后的指针或引用,否则返回空指针或抛出非常。例如:
class Base {public:virtual ~Base() {}};class Derived : public Base {public:void func() { cout << "Derived::func()" << endl; }};Base p = new Base;Derived q = dynamic_cast<Derived>(p);if (q != nullptr) {q->func();} else {cout << "Cast failed." << endl;}
在上述示例中,p是指向Base工具的指针,由于Base不是Derived的基类,因此无法进行类型转换,q将为nullptr,输出提示信息。
reinterpret_castreinterpret_cast用于指针之间的逼迫类型转换,可以将任何类型的指针转换为任何其他类型的指针,但是可能会导致不可预期的行为,因此该当慎重利用。
reinterpret_cast的语法格式如下:
new_type reinterpret_cast <type> (expression);
个中,new_type是要转换的指针类型,type是要转换的指针类型,expression是要转换的指针。例如:
int n = 10;void p = reinterpret_cast<void>(&n);int q = reinterpret_cast<int>(p);
在上述示例中,首先将一个整数的地址转换为void类型的指针p,然后再将其转换为int类型的指针q。由于reinterpret_cast没有进行任何标准转换,因此可能会导致不可预期的行为。
const_castconst_cast用于去除常量性,可以将常量类型转换为非常量类型,或者将非常量类型转换为常量类型。
const_cast的语法格式如下:
new_type const_cast <type> (expression);
个中,new_type是要转换的类型,type是要转换的类型,expression是要转换的工具指针或引用。例如:
const int n = 10;int p = const_cast<int>(&n);p = 20;cout << n << endl; // 输出10
在上述示例中,首先定义了一个常量整数n,然后利用const_cast将其转换为非常量整数指针p,末了修正p的值为20。由于n是常量,因此无法直接修正其值,但是通过const_cast将其转换为非常量指针后,可以修正其值。须要把稳的是,修正常量类型的值可能会导致不可预期的行为,该当慎重利用。
总结C++供应了四种不同类型的逼迫类型转换函数,分别是static_cast、dynamic_cast、reinterpret_cast和const_cast。它们各自适用于不同的场景,须要根据详细的需求进行选择。须要把稳的是,逼迫类型转换可能会导致不可预期的行为,该当慎重利用,并且担保类型转换的安全性。
参考资料:https://learn.microsoft.com/zh-cn/cpp/cpp/static-cast-operator?view=msvc-170