哪种铸造技术更适合在C ++中从上层类转换为下层类 [英] Which casting technique is better for doing casting from upper class to lower class in C++
本文介绍了哪种铸造技术更适合在C ++中从上层类转换为下层类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我应该使用Dynamic_cast或reinterpret_cast ?请告诉我
解决方案
不要使用 reinterpret_cast
code> static_cast 或 dynamic_cast
。如果你确定指针正是那个派生类对象使用 static_cast
,否则使用 dynamic_cast
需要基类是polymorhic),并检查结果,以确保指针确实是你想要的类。
i want to cast from upper class pointer to lower class i.e from the base class pointer to derived class pointer.
Should i use "Dynamic_cast" or "reinterpret_cast"? please advice me
解决方案
Don't use reinterpret_cast
- either use static_cast
or dynamic_cast
. If you're sure that the pointer is to exactly that derived class object use static_cast
, otherwise use dynamic_cast
(that will require the base class to be polymorhic) and check the result to ensure that the pointer is indeed to the class you want.
这篇关于哪种铸造技术更适合在C ++中从上层类转换为下层类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文