C++ slicing in Java / C# [duplicate]

本秂侑毒 提交于 2019-12-07 10:55:46

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/536267/c-slicing-in-java-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!