Cast object from derived to base and than back

2019-04-28 07:04发布

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 ?

标签: c++ oop casting
1条回答
ゆ 、 Hurt°
2楼-- · 2019-04-28 07:50

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.

查看更多
登录 后发表回答