C++中用dynamic_cast实现downcast之一见

Almost   alway   it   will   be   on   the   right   track   if   we   use   "dynamic_cast "   for   up-casting,   with   either   a   pointer   or   a   reference,   to   a   type   of   sub-class   from   that   of   base-class,   and   this   would   be   guaranteed   to   be   safe   in   most   cases.   Nevertheless   possibly,   if   you   ever   deploy   it   by   yourself,   it   will   not   act   as   you   expect   if   you   use   this   casting   operator   for   down-casting   instead.   For   me,   not   so   that   lucky   it   is!

  Now   let 's   show   you   the   scenario   as   this:   Originally   I   dynamically   create   an   object   of   the   sub-class   and   assign   the   returned   pointer   to   a   pointer   to   the   base-class,   then   actually   I   just   intend   to   cast   this   pointer   to   base-class   back   to   pointer   to   the   sub-class,   which   seems   absolutely   reasonable   and   nothing   unpredictable   would   be   expected,   but   thing   only   to   run   out   of   its   normal   way!

  Passing   through   the   compilation   is   entirely   ok,   but   when   I   trigger   the   program,   an   run-time   error   bursts   out,   which   is   concerning   such   and   such   a   memory   allocation   on   the   heap.   Then   I   try   the   same   case   for   downcasting   a   reference   instead   of   pointer,   the   result   stands   the   same,   in   the   end   ,therefore,   I   am   buffled!

  After   nearly   a   week   of   being   bewildered   and   puzzled,   edificed   by   a   post   issued   from   a   fellow   CSDNer,   I   recall   a   critical   try   --   the   compilation   option   that   controls   the   support   for   RTTI.   Woo!   I   cry,   doesn 't   the   dynamic-cast   depend   on   the   sustenance   of   RTTI   by   the   run-time   system?  
   
  Definitely   that 's   the   key   I 've   been   dredging   toiling   to   search!   Immediately   after   I   open   the   option   in   the   compilation   configuration,   everything   goes   extremely   right! 
 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
dynamic_cast实现原理的底层机制可以根据引用和引用来解释。dynamic_cast用于将指向多态类型(包含虚函数)的指针或引用进行downcast或crosscast。它会在运行时进行类型检查来确定是否可以进行转换。 在实现上,dynamic_cast使用了类型信息(typeinfo)来进行运行时类型检查。每个多态类型都会有一个对应的typeinfo对象,其中包含了类型的相关信息,例如类型名称和继承关系。当使用dynamic_cast进行转换时,它会通过查询类型对象的typeinfo,来判断是否可以进行安全的转换。 具体地说,dynamic_cast会在进行转换前,先检查指针或引用所指向的对象的typeinfo是否与目标类型的typeinfo匹配。如果匹配成功,说明转换是合法的,dynamic_cast会返回目标类型的指针或引用;如果匹配失败,说明转换是不安全的,dynamic_cast会返回一个空指针或引发一个std::bad_cast异常。 相比于static_cast,dynamic_cast会在运行时进行类型检查,从而提供了更安全的转换。而static_cast则是在编译时进行类型检查,并且没有运行时的开销。此外,dynamic_cast还可以用于将多态虚基类转换为子类或相邻兄弟类,而static_cast则不能进行这种转换。 综上所述,dynamic_cast的底层原理是利用类型信息进行运行时类型检查来确定是否可以进行安全的转换。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [dynamic_cast实现原理](https://blog.csdn.net/LIJIWEI0611/article/details/120699526)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [[C++] - dynamic_cast介绍及工作原理、typeid、type_info](https://blog.csdn.net/zqxf123456789/article/details/106245816)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值