C++ 强制类型转换运算符(八股总结)

static_cast

  • 用于非多态类型的转换
  • 不执行运行时类型检查(转换安全性不如 dynamic_cast)
  • 通常用于转换数值数据类型(如 float -> int)
  • 可以在整个类层次结构中移动指针,子类转化为父类安全(向上转换),父类转化为子类不安全(因为子类可能有不在父类的字段或方法)

向上转换是一种隐式转换。

dynamic_cast

  • 用于多态类型的转换
  • 执行行运行时类型检查
  • 只适用于指针或引用
  • 对不明确的指针的转换将失败(返回 nullptr),但不引发异常
  • 可以在整个类层次结构中移动指针,包括向上转换、向下转换

const_cast

  • 用于删除 const、volatile 和 __unaligned 特性(如将 const int 类型转换为 int 类型 )

reinterpret_cast

  • 用于位的简单重新解释
  • 滥用 reinterpret_cast 运算符可能很容易带来风险。 除非所需转换本身是低级别的,否则应使用其他强制转换运算符之一。
  • 允许将任何指针转换为任何其他指针类型(如 char*int*One_class*Unrelated_class* 之类的转换,但其本身并不安全)
  • 也允许将任何整数类型转换为任何指针类型以及反向转换。
  • reinterpret_cast 运算符不能丢掉 const、volatile 或 __unaligned 特性。
  • reinterpret_cast 的一个实际用途是在哈希函数中,即,通过让两个不同的值几乎不以相同的索引结尾的方式将值映射到索引。

bad_cast

  • 由于强制转换为引用类型失败,dynamic_cast 运算符引发 bad_cast 异常。

主要用途及示例

1. static_cast

用于在相关类型之间执行编译时的强制转换(如基本数据类型之间的转换,指针类型的转换)。

#include 
using namespace std;

int main() {
    double pi = 3.14159;
    int integerPi = static_cast<int>(pi);
    cout << "Original: " << pi << ", After static_cast: " << integerPi << endl;

    return 0;
}

输出:

Original: 3.14159, After static_cast: 3

2. dynamic_cast

用于在运行时对多态类型(带有虚函数的类)进行安全的向下类型转换。只有在转换安全时,转换才会成功。

#include 
using namespace std;

class Base {
    virtual void dummy() {}
};

class Derived : public Base {
public:
    void show() { cout << "Derived class method" << endl; }
};

int main() {
    Base* basePtr = new Derived();
    Derived* derivedPtr = dynamic_cast<Derived*>(basePtr);

    if (derivedPtr) {
        derivedPtr->show();
    } else {
        cout << "Conversion failed" << endl;
    }

    delete basePtr;
    return 0;
}

输出:

Derived class method

3. const_cast

用于修改对象的constvolatile属性。

#include 
using namespace std;

void modify(const int* ptr) {
    int* modifiablePtr = const_cast<int*>(ptr);
    *modifiablePtr = 42;
}

int main() {
    const int val = 10;
    cout << "Before modify: " << val << endl;
    modify(&val);
    cout << "After modify: " << val << endl;

    return 0;
}

输出:
(行为未定义,具体表现可能因编译器不同而异,谨慎使用)


4. reinterpret_cast

用于执行低级别的、几乎不受限制的类型转换,例如指针类型之间的转换。

#include 
using namespace std;

int main() {
    int num = 65;
    char* charPtr = reinterpret_cast<char*>(&num);
    cout << "Interpreted as char: " << *charPtr << endl;

    return 0;
}

输出:

Interpreted as char: A

总结

(1)const_cast: 把const属性去掉,即将const转换为非const(也可以反过来),const_cast只能用于指针或引用,并且只能改变对象的底层const(顶层const,本身是const,底层const,指向对象const);

(2)static_cast: 隐式类型转换,可以实现C++中内置基本数据类型之间的相互转换,enum、struct、 int、char、float等,能进行类层次间的向上类型转换和向下类型转换(向下不安全,因为没有进行动态类型检查)。它不能进行无关类型(如非基类和子类)指针之间的转换,也不能作用包含底层const的对象;

(3)dynamic_cast:动态类型转换,用于将基类的指针或引用安全地转换成派生类的指针或引用(也可以向上转换),若指针转换失败返回NULL,若引用返回失败抛出bad_cast异常。dynamic_cast是在运行时进行安全性检查;使用dynamic_cast父类一定要有虚函数,否则编译不通过;

(4)reinterpret_cast:reinterpret是重新解释的意思,此标识符的意思即为将数据的二进制形式重新解释,但是不改变其值,有着和C风格的强制转换同样的能力。它可以转化任何内置的数据类型为其他任何的数据类型,也可以转化任何指针类型为其他的类型。它甚至可以转化内置的数据类型为指针,无须考虑类型安全或者常量的情形。不到万不得已绝对不用(比较不安全)

static_cast和dynamic_cast的异同点?

答:二者都会做类型安全检查,只是static_cast在编译期进行类型检查,dynamic_cast在运行期进行类型检查。后者需要父类具备虚函数,而前者不需要。

你可能感兴趣的:(C++八股,c++,开发语言)