Question

Can C++ slicing apply to other languages too, like Java/C#?

Was it helpful?

Solution

Slicing means that if you assign a subclass instance to a superclass variable, the extra information contained by subclass is "sliced" off, because the superclass variable doesn't have the extra space to store this extra information of the subclass.

This doesn't happen in Java nor with C#, because all object variables are references; when you assign a subclass instance to a superclass variable, you actually just copy the reference; the subclass object itself remains intact.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top