tags:

views:

674

answers:

5

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

+5  A: 

This is a duplicate. See What is the slicing problem in C++?.

codelogic
Some of it is a duplicate. The title. What about the body of the question, though?
Rob Kennedy
You're right, it's not an exact duplicate. IMHO it would be more helpful to others if a new, more general, question asking about slices in C# and Java were asked, because the title here seems misleading.
codelogic
@codelogic your are welcome to change the title
yesraaj
+8  A: 

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.

Joonas Pulakka
"This doesn't happen in Java or C#, because they use pointers to reference objects" - this is quite incorrect.
duffymo
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.
Joonas Pulakka
Your comment wasn't very helpful, Duffymo.
Rob Kennedy
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.
Johannes Schaub - litb
ah never mentioned. you can't derive a struct from another struct in C#
Johannes Schaub - litb
You're right. My statement was incorrect regarding C# value types -that is, structs. For classes it's still correct.
Joonas Pulakka
Or something... I think I'll have some coffee now ;-)
Joonas Pulakka
just one thing: duffymo, your comment was useless :D and i now see you said "subclass" so i guess that would already rule out structs :) i should have read more closely. i'm sorry mad-j. anyway i will +1 this one :)
Johannes Schaub - litb
My point is that slicing happens in both Java and C#. The OP said it doesn't happen. Either I've misunderstood the question or the OP has.
duffymo
In Java if you assign a subclass to a superclass var you can grab the subclass' data if you cast the superclass to the subclass. It's not removed.e.g.Inner i = new Inner(5);Outer o = i;System.out.println((Inner)o.getValFromInnerConstructorArg());I think gives:5
Robert Grant
duffymo, can you give an example of object slicing happening in Java? I don't understand how it could happen, since objects are automatically allocated the memory that they need. The memory allocation is not affected by the type of the reference variable.
Joonas Pulakka
+2  A: 

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,

Rob Wells
I have one :-) ..
yesraaj
A: 

Checkout this link

yesraaj
A: 

Here's how I understand the slicing problem:

I start with a class Parent, with a method doThis.

I create a new class Child, which inherits the doThis method and defines another method doThat.

I now create an array of Parent references and populate it with references to Parent and Child. If I iterate through that array of Parent references, I can't call the doThat method on any reference without knowing its underlying type and casting to Child where appropriate. I can call doThis on every reference without concern, because every Child IS-A Parent and has a doThis method to call.

The representation of the Child class isn't modified in memory, but the doThat method isn't available via a Parent reference without casting - it's "sliced away".

Perhaps I'm incorrect, but that's how I think of the slicing problem. If that's true, it is indeed present in Java.

duffymo
You understanding is incorrect. Slicing deals with actual LOSS of data. In your example, the method hasn't been sliced away since you still have Child references. Type-cast the reference, and you'll find that the whole object is still there.
Rob Kennedy