Cast object from derived to base and than back

2019-04-28 07:39发布

问题:

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 ?

回答1:

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.



标签: c++ oop casting