Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 816
yesraaj Avatar asked Feb 11 '09 10:02

yesraaj


People also ask

Can you do slicing in Java?

JavaScript Array slice() The slice() method selects from a given start, up to a (not inclusive) given end. The slice() method does not change the original array.

Is there slicing in C++?

Object Slicing in C++When a derived class object is assigned to a base class object in C++, the derived class object's extra attributes are sliced off (not considered) to generate the base class object; and this whole process is termed object slicing.

What is object slicing explain with example?

"Slicing" is where you assign an object of a derived class to an instance of a base class, thereby losing part of the information - some of it is "sliced" away. For example, class A { int foo; }; class B : public A { int bar; }; So an object of type B has two data members, foo and bar .


1 Answers

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.

like image 102
Joonas Pulakka Avatar answered Nov 14 '22 22:11

Joonas Pulakka