C++ 11 新特性 不同数据类型之间转换函数之static_cast

本文详细介绍了C++中的静态转换函数static_cast,包括基本数据类型之间的转换、枚举变量与整数的互转以及基类指针与子类指针的转换实例,展示了其在编程中的实用价值。
摘要由CSDN通过智能技术生成

一.不同数据类型之间转换函数static_cast介绍

        static_cast:这是C++中最常用的类型转换操作符,用于在相关类型之间进行转换,例如非const转换为const,或者父类指针转换为子类指针。

二.static_cast 函数使用示例

(1)基本数据之间转换 

#include <iostream>

enum Color {
    RED = 1,
    GREEN = 2,
    BLUE = 3
};

int main() {
    int num = 42;
    double result = static_cast<double>(num); // 将整数转换为浮点数

    int* ptr = nullptr;
    void* voidPtr = static_cast<void*>(ptr); // 将指针转换为void指针

    Color color = RED;
    int colorValue = static_cast<int>(color); //将枚举变量转换为整数
    std::cout << "The integer value of the color is: " << colorValue << std::endl;

    int cnt = 2;
    Color clr = static_cast<Color>(cnt);       //整数转换为枚举变量
    std::cout << "color: " << clr << std::endl;

    return 0;
}

运行结果:

 

这表明整数成功转换为枚举变量,并且枚举变量的值为2。 

(2) 使用static_cast进行基类指针子类指针之间相互转换

演示如何将基类指针 转换为 子类指针

#include <iostream>

class Base {
public:
    void print() {
        std::cout << "Base class" << std::endl;
    }
};

class Derived : public Base {
public:
    void print() {
        std::cout << "Derived class" << std::endl;
    }
};

int main() {
    Base baseObj;
    Derived* derivedPtr = static_cast<Derived*>(&baseObj); // 基类指针转换为子类指针
    if (derivedPtr != nullptr)
    {
        derivedPtr->print(); // 调用派生类的print函数
    }
    else
    {
        std::cout << "Conversion failed" << std::endl;
    }
    return 0;
}

运行结果:

这表明基类指针成功转换为子类指针,并调用了子类的print函数。  

演示如何将子类指针 转换为 基类指针

#include <iostream>

class Base {
public:
    void print() {
        std::cout << "Base class" << std::endl;
    }
};

class Derived : public Base {
public:
    void print() {
        std::cout << "Derived class" << std::endl;
    }
};

int main() {
    Derived derivedObj;
    Base* basePtr = static_cast<Base*>(&derivedObj); // 子类指针转换为基类指针
    basePtr->print(); // 调用基类的print函数
    return 0;
}

运行结果:

 

这表明子类指针成功转换为基类指针,并调用了基类的print函数。 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,四种类型的转换(有时也称为“cast”)用于处理不同数据类型的互相转换,它们各有特定的用途和使用场景。以下是这四种转换的简要说明以及它们在源代码中的基本实现: 1. **static_cast**:这是一种静态类型转换,用于明确且不涉及运行时类型的检查。它可以用于基本类型、指针、引用和内置的enum之间直接的转换。源代码示例: ```cpp int i = 5; double d = static_cast<double>(i); ``` 2. **dynamic_cast**:用于运行时类型检查,主要用于对象之间的类型转换,特别是当基类指针或引用需要转换为派生类类型时。如果类型转换成功,返回指向或引用的对象;否则返回null(对非指针)。例如: ```cpp class Base { ... }; class Derived : public Base { ... }; Base* baseObj = new Derived(); Derived* derivedObj = dynamic_cast<Derived*>(baseObj); ``` 3. **const_cast**:用于修改对象的const属性,即使在常量表达式中也可以用于临时去除const限定。这通常用于函数参数传递或返回值,或者为了修改const成员。注意这是有风险的,因为它打破了const契约: ```cpp const int& constRef = getConstInt(); int& mutableInt = const_cast<int&>(constRef); ``` 4. **reinterpret_cast**:这是最强大但也是最危险的转换,它将一个对象的数据重新解释为另一种类型,无视数据原有的字节布局。通常用于底层内存操作,如指针类型间的转换。不建议用于正常类型转换,仅在必要时使用,例如: ```cpp void* rawMemory = ...; // 假设我们知道它是int数组 int* intPtr = reinterpret_cast<int*>(rawMemory); ``` 请注意,这些类型转换在编译时就会进行,因此源代码中的实现主要体现在编译器层面,而不是直接在源码文件中看到具体的转换操作。实际的转换操作在C++库内部由编译器优化器处理。在代码中,我们主要关注的是转换的语法和意图。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值