将对象从派生投射到基础,然后再投射 [英] Cast object from derived to base and than back

查看:57
本文介绍了将对象从派生投射到基础,然后再投射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将对象转换为基本类型并将其存储在std :: map中,然后将其取回并重铸为派生类型,则派生类成员中是否仍具有正确的数据?

我需要进行特定类型的演员吗?

解决方案

如果您要投射指向该对象的指针,则没问题,因为指针仍将指向同一对象.

如果要投射实际对象,则子类中的所有信息在转换为超类时都将丢失,因此将其投射回后将无法恢复该信息.

If i cast an object to the base type and store it in a std::map, and then get it back and recast it to the derived type, do i still have the correct data in the derived class members ?

Do i need to make a specific kind of cast ?

解决方案

If you're casting pointers to the object, it's no problem because the pointers will still point to the same object.

If you're casting the actual object, all information from the subclass will be lost when its converted to the superclass, so casting it back won't restore that information.

这篇关于将对象从派生投射到基础,然后再投射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆