Explain Object Slicing and how it occurs when passing objects by value in an inheritance hierarchy.
Last updated
Object slicing: copying a derived object into a base object by value keeps only the base part, dropping derived data and dynamic type.
After slicing, even
virtualcalls run the base version, because the base copy constructor knows nothing of the derived part.Avoid it by passing and storing polymorphic objects by reference (
const Base&) or pointer (including smart pointers).