reinterpret_cast和static_cast的总结
生活随笔
收集整理的這篇文章主要介紹了
reinterpret_cast和static_cast的总结
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
主要參考:http://blog.csdn.net/querw/article/details/7387594
http://www.cnblogs.com/jerry19880126/archive/2012/08/14/2638192.html
http://www.cnblogs.com/ider/archive/2011/07/30/cpp_cast_operator_part3.html
http://bbs.csdn.net/topics/390249118
關(guān)鍵點(diǎn):
1.reinterpret_cast:?reinterpret“重新解釋”內(nèi)存類型?
“重新解釋了數(shù)據(jù)的比特模型,并沒(méi)有進(jìn)行二進(jìn)制的轉(zhuǎn)換”
static_cast ?: 編譯時(shí)強(qiáng)制轉(zhuǎn)換,進(jìn)行安全性檢查、截?cái)唷⒅羔樒?#xff1b;
2.注意轉(zhuǎn)換是長(zhǎng)對(duì)短,下對(duì)上;還是短對(duì)長(zhǎng),上對(duì)下轉(zhuǎn)
比如
Struct?CBaseX {int?x; } Struct?CBaseY {int?y; } Struct?CDerived:CBaseX,CBaseY{} 進(jìn)行static_cast<CBaseY*>(&d),//此時(shí)進(jìn)行了指針的偏移,如下圖所示,偏移了baseX的四個(gè)字節(jié) //如果是reinterpret_cast則不會(huì)進(jìn)行偏移。轉(zhuǎn)載于:https://blog.51cto.com/chenfei/1652010
總結(jié)
以上是生活随笔為你收集整理的reinterpret_cast和static_cast的总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: AngularJS入门心得2——何为双向
- 下一篇: shell timeout