dynamic_cast,static_cast repreint_cast const_cast

dynamic type:

  比如

  

class A { }; class B : public A { }; B l; A& k = l;

Here k is a reference to an object of type A, but the real type of the referred object, its dynamic type, is B.

Here "dynamic" has the meaning of "known only at run-time".

 

所以根据文档:

  dynamic_cast会进行动态类型检查,只有符合运行时类型的对象才能转换,转换失败会返回NULL(*)或者抛异常std::bad_cast.(&),而static_cast不会检查类型正确性。

  dynamic_cast 可以classes up, down, and sideways along the inheritance hierarchy.

  B* a = dynamic_cast <B*>l; // correct

  B b = B;

  B* a = dynamic_cast <B*>; // return null

 

static_cast 就像c风格的强制类型转换(int)一样,repreint_cast用于指针转换,也和c风格很相似,const_cast用于将类型的const性去掉或装上,但是将原本是const的对象转成const的行为是未定义的,由程序员自己把控。

 

转载于:https://www.cnblogs.com/yiii/p/7934302.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值