7

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

3
  • <strike>This is a duplicate</strike>. See What is the slicing problem in C++?. Commented Feb 11, 2009 at 10:40
  • Raj, buy a copy of Scott Meyer's excellent book "Effective C++" (sanitised Amazon link) for many excellent discussions about this problem and many other C++ gotchas. HTH cheers, Commented Feb 11, 2009 at 10:44
  • 1
    This is not an exact duplicate. The supposed duplicate asks nothing about C# or Java. Commented Feb 12, 2009 at 0:36

1 Answer 1

15

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.

Sign up to request clarification or add additional context in comments.

10 Comments

My terminology was a bit off (corrected it now), but I think my point wasn't. If I'm still quite incorrect, could you please correct.
Your comment wasn't very helpful, Duffymo.
C# has value types. i think in c# your statement isn't true (? i'm a c# noob. so i can be wrong here), but for java it's of course very true.
ah never mentioned. you can't derive a struct from another struct in C#
You're right. My statement was incorrect regarding C# value types -that is, structs. For classes it's still correct.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.