What is object slicing?


Question

Someone mentioned it in the IRC as the slicing problem.

1
695
11/13/2018 10:37:27 AM

"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.

Then if you were to write this:

B b;

A a = b;

Then the information in b about member bar is lost in a.

575
5/11/2016 7:47:53 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon