C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++强制类型转换

C++强制类型转换详细示例代码

作者:EnigmaCoder

在C++中,强制类型转换(Type Casting)是将一种数据类型转换为另一种数据类型的操作,这篇文章主要介绍了C++强制类型转换的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下

前言

static_cast:最常用的“非多态”转换

static_cast最常用 的类型转换运算符,主打 “非多态场景” 的类型转换,编译期间就会做合法性检查,能替代 C 语言中普通的类型转换。

适用场景

  1. 基本数据类型之间的转换(如 intfloat 互转);
  2. 将任意类型转换为 void 类型;
  3. 空指针(或 void*)与具体类型指针的转换;
  4. 类继承体系中 “向上转换”(派生类 → 基类,天然安全)。

代码示例

#include <iostream>
using namespace std;

int main() {
    // 1. 基本数据类型转换
    float floatNum = 3.14f;
    int intNum = static_cast<int>(floatNum);
    cout << "float → int: " << intNum << endl; // 输出:3(小数被截断)

    intNum = 5;
    floatNum = static_cast<float>(intNum);
    cout << "int → float: " << floatNum << endl; // 输出:5.0

    // 2. 空指针与具体指针转换
    void* voidPtr = nullptr;
    int* intPtr = static_cast<int*>(voidPtr);
    cout << "void* → int*: " << intPtr << endl; // 输出:0(nullptr)

    return 0;
}

注意点

reinterpret_case:底层“位模式”的重新解释

reinterpret_cast 主打 “底层位模式的重新解释”,它不做数据转换或类型检查,完全是“按位硬转”,因此风险很高,仅用于特殊底层场景。

适用场景

代码示例

#include <iostream>
using namespace std;

int main() {
    // 1. 字符 → 整数(直接解释 ASCII 位模式)
    char ch = 'A'; // 'A' 的 ASCII 码是 65
    int ascii = reinterpret_cast<int&>(ch);
    cout << "char → int(ASCII 值): " << ascii << endl; // 输出:65

    // 2. 指针类型的底层转换(int* → float*)
    int* intPtr = nullptr;
    float* floatPtr = reinterpret_cast<float*>(intPtr);
    cout << "int* → float*: " << floatPtr << endl; // 输出:0(与 intPtr 地址一致)

    return 0;
}

注意点

const_cast:专门“移除 const 限定”

const_cast 是“专项工具”——专门用于移除指针或引用的 const(或 volatile)属性,让原本被 const 限制的指针/引用能修改指向的内容。

适用场景

代码示例

#include <iostream>
using namespace std;

int main() {
    int num = 100; // 注意:若原对象本身是 const,修改会出问题,这里用非 const 变量演示
    const int* constPtr = &num;

    // 移除指针的 const 属性
    int* normalPtr = const_cast<int*>(constPtr);
    *normalPtr = 200;
    cout << "修改后 num: " << num << endl; // 输出:200

    // 移除引用的 const 属性
    const int& constRef = num;
    int& normalRef = const_cast<int&>(constRef);
    normalRef = 300;
    cout << "再次修改后 num: " << num << endl; // 输出:300

    return 0;
}

注意点

  • 如果原始对象本身声明为 const(如 const int num = 100;),用 const_cast 移除 const 后再修改它,会导致“未定义行为”(程序可能崩溃或结果异常)。

dynamic_cast:运行时的“多态安全转换”

dynamic_cast 主打 “多态类(含虚函数)的运行时类型检查”,主要用于类继承体系中“向上转换”(派生 → 基类)或“向下转换”(基类 → 派生),保证转换的安全性。

适用场景

代码示例

#include <iostream>
using namespace std;

// 基类:必须包含虚函数,才能启用“运行时类型识别(RTTI)”
class Base {
public:
    virtual void show() { cout << "Base class" << endl; }
};

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

int main() {
    // 场景1:基类指针指向派生类对象(向下转换合法)
    Base* basePtr1 = new Derived();
    Derived* derivedPtr1 = dynamic_cast<Derived*>(basePtr1);
    if (derivedPtr1 != nullptr) {
        cout << "dynamic_cast(Base*→Derived*)成功: ";
        derivedPtr1->show(); // 调用 Derived 的 show()
    }

    // 场景2:基类指针指向基类对象(向下转换非法,返回 nullptr)
    Base* basePtr2 = new Base();
    Derived* derivedPtr2 = dynamic_cast<Derived*>(basePtr2);
    if (derivedPtr2 == nullptr) {
        cout << "dynamic_cast(Base*→Derived*)失败(符合预期)" << endl;
    }

    // 释放动态内存
    delete basePtr1;
    delete basePtr2;

    return 0;
}

注意点

  • dynamic_cast 依赖 “运行时类型信息(RTTI)”,因此基类必须包含虚函数(否则编译报错);
  • 向下转换时,若基类指针实际指向派生类对象,转换成功;否则返回 nullptr(指针场景)或抛出异常(引用场景)。

总结:四种转换的分工

转换运算符核心作用场景特点
static_cast日常非多态转换,编译期检查最常用、安全(非向下转)
reinterpret_cast底层位模式重新解释风险高,仅底层场景用
const_cast专门移除 const 限定专项工具,需谨慎修改
dynamic_cast多态类的运行时安全转换依赖虚函数,运行时检查

1 使用dynamic_cast<>的情况

类层次结构中父类和子类之间指针和引用的转换(运行时,类型检查,保证了程序的安全性)【可以用于基类指针指向派生类的指针】
其他情况使用static_cast<>。

2 使用static_cast<>的情况

到此这篇关于C++强制类型转换的文章就介绍到这了,更多相关C++强制类型转换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文