C++中四种类型转换方式

1、静态转换(static_cast)
 
static_cast包含的转换类型有 典型的非强制变换窄化(有信息丢失)变换使用void*的强制转换隐式类型变换类层次的静态定位。static_cast是编译器允许的。
 
(1)典型的非强制变换:
从窄类型向宽类型的转换,如char向short int,int,long int,float,double,long double的转换。
char a = 1;
long b = static_cast<long>(a);
 
(2)窄化变换:
与第1种相反,从宽类型向窄类型的变换。
long b = 1;
char a = static_cast<char>(b);
 
(3)使用void*的强制变换:
struct callback_param
{
    void *vp;
};
 
int a = 1;
struct callback_param cp;
cp.vp = &a;      //编译器允许从任意类型指针向void*转换
 
int *ip = static_cast<int *>(cp.vp);
 
(4)隐式类型转换:
包括(1)(2)
 
(5)类层次的静态定位
进行向上类型转换(派生类向基类转换)时,编译器清楚派生自什么祖先类,除了多继承(多继承转换后可能不为原地址,指针会在类层次中调整)。
 
2、常量转换(const_cast)
从const转换为非const,从volatile转换为非volatile。取得const对象的地址,会生成一个指向const的指针,volatile同。
 
const int i = 0;
int *ip = const_cast<int *>(&i);
 
volatile int a = 0;
int *ap = const_cast<int *>(&a);
 
3、重解释转换(reinterpret_cast)
interpret是解释的意思,reinterpret即为重新解释,此标识符的意思即为数据的二进制形式重新解释,但是不改变其值。如: int i; char *ptr="hello freind!"; i=reinterpret_cast<int>(ptr); 这个转换方式很少使用。
最不安全的一种转换机制,将对象转变为完全不同类型的对象,这是低级的位操作。
 
struct msg_hdr
{
    int msg_type;
    int msg_len;
    char msg_data[0];
};
 
struct msg_data
{
    int data1;
    int data2;
};
 
struct msg_hdr *p = reinterpret_cast<struct msg_hdr *>(recvbuf);
struct msg_data *pdata = reinterpret_cast<struct msg_data *>(p->msg_data);
 
4、动态转换(dynamic_cast)
类层次的向下转换(基类向派生类转换),转换过程中会通过RTTI检查转换类型是否正常,不正常将返回空。
该操作符用于运行时检查该转换是否类型安全,但只在多态类型时合法,即该类至少具有一个虚拟方法。dynamic_cast与static_cast具有相同的基本语法,dynamic_cast主要用于类层次间的上行转换和下行转换,还可以用于类之间的交叉转换。在类层次间进行上行转换时,dynamic_cast和static_cast的效果是一样的;在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全。

#include <iostream>
using namespace std;

class pet
{
    public:
    virtual ~pet()
    {
    }
};

class dog : public pet
{
};

class cat : public pet
{
};

int main(void)
{
    pet *b = new cat;

    dog *d1 = dynamic_cast<dog *>(b);
    cat *d2 = dynamic_cast<cat *>(b);

    cout << "d1 = " << (long)d1 << endl;  // d1 = 0
    cout << "d2 = " << (long)d2 << endl;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值