C++的类型转换

目录

四种类型转换

static_cast (ret)

dynamic_cast (ret)*、&>

const_cast (ret)*、&>

teinterpret (ret)


四种类型转换

        静态类型转换:static_cast <Type> (ret)

        动态类型转换:dynamic_cast <Type*、&> (ret)

        常引用/指针的类型转换:const_cast <Type*、&> (ret)

        解释类型转换:teinterpret <Type> (ret)

static_cast <Type> (ret)

        静态类型转换。

        提供了编译器认为有联系有关系类型之间的安全转换。

        是否安全,也要由开发人员保证。

        应用场景

        1.C++中的基本类型转换,与强转类似。如float转int等。

                不可用于基本类型指针间的类型转换

        2.在继承关系的情况下的层次结构间的转换

           向上转型:即子类向父类转型,安全,无需static_cast

           向下转型:即父类向子类转型,可能存在内存类域外,不安全

        

        代码示例

        定义一个父类一个子类:

#include <iostream>
using namespace std;
class A
{
public:
    int a;
    int b;
public:
    A(int a, int b)
    {
        this->a = a;
        this->b = b;
    }
    void showInfo_A()
    {
        cout << this->a << "," << this->b << endl;
    }
};

class B : public A
{
public:
    string name;
    string xxx;
    B() : A(1,3)
    {
        this->name = "zhangsan";
        this->xxx = "lisi";
        cout << "B 的构造" << endl;
    }
    void showInfo_B()
    {
        cout << this->a <<"," << this->b << endl;
        cout << this->name << "," << this->xxx << endl;
    }
};

        主函数验证:

int main(int argc, char *argv[])
{
    double d = 3.14;
    cout << "普通强转:" << (int )d << endl; //3
    cout << "静态强转" << static_cast<int >(d) << endl; //3
    cout << "-------------------1" << endl;

    double *pd = &d;
    cout << *pd << endl; //3.14
    int * pa = (int *) &d;
    cout << "指针强转" << *pa << endl; //1374389535普通强转不安全
    cout << "--------------2" << endl;

    //此方法报错,说明,static_cast不可用于指针,有报错机制
// cout << static_cast<int *>( &d) << endl;
    A* a = new A (1,3);
    a->showInfo_A();
    cout << "--------------4" << endl;

    //此两种方法乱码,原因是子类空间未开辟
// ((B*)a)->showInfo_B(); //父类向子类转
// static_cast<B*>(a)->showInfo_B();

    B* b = new B();
    ((A*)b)->showInfo_A(); //子类向父类天然安全

    return 0;
}

        运行结果:

        

 

dynamic_cast <Type*、&> (ret)

        动态类型转换。

        应用场景

        用于多态,可实现安全的上下层转换

        向下转换时(依赖多态):要有继承关系,且要有虚函数的层次结构的转换。(安全的向下转型方式)

        代码示例

        定义一个父类一个子类:

#include <iostream>
using namespace std;

class A
{
public:
    int a;
    int b;
public:
    A(int a,int b)
    {
        this->a = a;
        this->b = b;
    }
    void showInfo_A()
    {
        cout << this->a <<"," << this->b << endl;
    }
    virtual ~A()
    {
    }
};

class B : public A
{
public:
    string name;
    string xxx;
    B():A(1,3)
    {
        this->name = "zhangsan";
        this->xxx = "lisi";
        cout << "B的构造" << endl;
    }
    void showInfo_B()
    {
        cout << this->a <<"," << this->b << endl;
        cout << this->name << "," << this->xxx << endl;
    }
};

        主函数验证:

int main()
{
    A* a = new B();
    a->showInfo_A();
    ((B*)a)->showInfo_B();
    cout << "--------------------1" << endl;

    //当使用动态转换时,向下转换时,会首先验查虚表中RRRI信息,
    //如果不存在向下转换的类型,
    //dynamic_cast将直接返回一个nullptr空指针
    A* a2 = new A(5,6);
    dynamic_cast<B*>(a2)->showInfo_B(); //无现象
    return 0;
}

        运行结果:

        

 

const_cast <Type*、&> (ret)

        常引用/指针类型转换。

        应用场景

        1.用于const修饰指针或引用的类型转换为非const修饰指针或引用。

        2.不可用于非指针或引用类型的常类型转换。

        3.不能偏移 * const  p 的指向

        4.注:变量本身的const属性是不能去除的,要想修改变量的值,一般是去除指针(或引用)的const属性,再进行间接修改。

       

        代码示例

        主函数验证:

#include <iostream>
using namespace std;
int main()
{
    int a =10;
    const int & b = a;
    const_cast<int &>(b) = 500;
    cout << "a" << a << endl; //500
    cout << "b" << b << endl; //500
    //证明const_cast可以修改const引用的值
    cout << "-----------------1" << endl;

    *((int *) &b) = 5000;
    cout << "a" << a << endl; //5000
    cout << "b" << b << endl; //5000
    //证明可通过 取引用 的地址,来修改引用的值
    cout << "-----------------2" << endl;
   int * const p = &a;
    int c = 88;
    * const_cast<int *>(p) = c;
    cout << "a" << a << endl; //88
    cout << "b" << b << endl; //88
    //以上方法,与普通修改没什么两样

// const_cast<int *>(p) = &c;
    //此方法不可行,*const p 依然不能修改
    //即不能偏移指针,
    //不支持const修饰的左值
    cout << "-----------------3" << endl;

    const int * p2 = &a;
    //*p2 =99;
    *(const_cast<int *>(p2)) = 99;
    cout << "a" << a << endl; //99
    cout << "b" << b << endl; //99

    //证明可以修改,const *p 里的值
    //证明,const_cast可以修改指针或引用里的值,
    //但不能偏移指针
    return 0;
}

        运行结果:

        

 

teinterpret <Type> (ret)

        解释类型转换。

        应用场景

        1.任意类型间的转换。

        2.比较底层的强制转换,没有任何的类型检查和格式转换,仅仅是简单的二进制数据的拷贝。

        3.从类型对象的曾二进制位重新解析后的转换是极不安全的强转,非大师不用。

        代码示例

        主函数验证:

#include <iostream>
using namespace std;
int main()
{
    int a = 10;
    int* p = &a;
    cout << reinterpret_cast<long long>(p) << endl;//底层二进的转换方式。
    return 0;
}

        运行结果: 

        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值