Do You Know "Object Slicing"?
When you try to copy a subclass object to a superclass object, the superclass object does not have any member variables and member functions in the subclass. Therefore these variables and functions have been “sliced offâ€.
For example, let’s assume the class hierarchy is something like this.
Then, if you assign “B†object to “Aâ€, you will lose all the attributes of “Bâ€.
Slicing is allowed by the compilers, which is highly discouraged because this will make the code more unmaintainable.
Here the object “a†does not have any unique features of “Bâ€. Hence it behaves like an “A†object.
Some experts say, slicing could cause memory corruption.?