[Solved] How to prevent object should not be slice [closed]


UPDATE:

My initial thoughts as described below were found as not true:
See: C++ slicing in Java / C#


Original Answer:

If I understood correctly this is a theoretical question.
REDUCING slicing can be done by not defining new members in the derived class.

slicing occurs when assigning an instance of a derived class to a base class reference type.
In this case the system does not know where to allocate a new data-type because it is defined in the derived class only and not in the base class.

But there is no way to RESTRICT that, this is what suppose to happen and it is defined as

A natural and unavoidable consequence of assignment by value from
subclass objects

Object slicing – Wikipedia

however you can declare a class as sealed to prevent inheritance that will restrict object slicing because it will prevent the cause (Inheritance) so you will not have a derived class to assign to a base reference by value.

4

solved How to prevent object should not be slice [closed]