9

It is mentioned in C++ FAQ site -- "larger derived class objects get sliced when passed by value as a base class object", what does slicing mean? Any sample to demonstrate?

http://www.parashift.com/c++-faq-lite/value-vs-ref-semantics.html#faq-31.8

I am using VSTS 2008 + native C++ as my development environment.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
George2
  • 44,761
  • 110
  • 317
  • 455
  • 2
    Here is something i found the other day: `struct Food { int calories; }; struct Bread : Food { int butter; }; Food food = Bread(); // Slicing bread has never been easier...` – Johannes Schaub - litb Mar 13 '10 at 14:23
  • I think slicing means converting from derived class object to base class object. I think it is very common operation in C++, do you think slicing is a natural feature or a bad design? – George2 Mar 16 '10 at 13:50
  • Possible duplicate of [What is object slicing?](http://stackoverflow.com/questions/274626/what-is-object-slicing) –  Apr 07 '16 at 22:47

4 Answers4

13

Quoting this lecture:

Slicing

Suppose that class D is derived from class C. We can think of D as class C with some extra data and methods. In terms of data, D has all the data that C has, and possible more. In terms of methods, D cannot hide any methods of C, and may have additional methods. In terms of existing methods of C, the only thing that D can do is to override them with its own versions.

If x is an object of class D, then we can slice x with respect to C, by throwing away all of the extensions that made x a D, and keeping only the C part. The result of the slicing is always an object of class C.

slicing http://webdocs.cs.ualberta.ca/~hoover/Courses/201/201-New-Notes/lectures/slides/slice/slide1.gif

Design Principle: Slicing an object with respect to a parent class C should still produce a well-formed object of class C.

Usage Warning: Even though D is-a C, you must be careful. If you have a argument type that is a C and you supply a D it will be sliced if you are doing call by value, pointer, or reference. See the example below.

Note on virtual functions. Their signatures are used to identify which one to execute.

Watch out for the sliced = operator, it can make the lhs inconsistent. Also, the operator= is never virtual, it wouldn't make sense. For example, suppose classes A, B are both subclasses of class C. Just because an A is a C, and a B is a C, it doesn't mean you can assign a B object to an A object. Without run-time type information you cannot make a safe assignment.

Jacob
  • 34,255
  • 14
  • 110
  • 165
  • What do you mean "Their signatures are used to identify which one to execute."? – George2 Mar 12 '10 at 15:48
  • 1
    And what do you mean by "it can make the lhs inconsistent"? If D is derived from C, then a sliced D should be a valid C. Otherwise you're doing inheritance wrong. – David Thornley Mar 12 '10 at 16:03
  • Slicing is good design or not? From your sample I think it is C++'s built-in feature. – George2 Mar 12 '10 at 16:05
  • @David, I think in C++, a sliced D should always be a valid C. Any cases when a sliced D is not a valid C? – George2 Mar 12 '10 at 16:06
  • 1
    @George2: Only when somebody's messed up seriously. A valid D should always be a valid C, and it certainly should stay that way when sliced. It would be possible for a D to mess up the base C, particularly if C used `protected` and not `private` data members, but it's not a good idea. – David Thornley Mar 12 '10 at 16:49
  • I take some time to think about your sample, and I am confused why you think which method M is used when we slice D to C is confused? Could you explain more? – George2 Mar 15 '10 at 05:40
5

Slicing means that the derived class information is lost. The base class parameter forces an object of the base class to created which share the same base-class state as the derived class. E.g:

struct B { int x; };

struct D : B { double y; };

void f(B b) {}

int main() {
  D d;
  f(d);
}

In the function f you will not be able to access D::y.

Edit: Thanks to everyone for editing. Please make sure that the edit adds value. Note that

  • For structs the default inheritance is public.
  • If you fix the code, make sure you update the body too
dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • Why D::y is lost? I think it is expected, since object of type B does not have member called y. – George2 Mar 12 '10 at 15:55
  • 1
    That is one way of looking at it. Think of virtual member functions. I think that'll help you see why I say that _information is lost_. – dirkgently Mar 12 '10 at 16:00
  • Could you explain more about why you think d::y is lost? I think it is not lost if you have another function which accepts type D as parameter. – George2 Mar 15 '10 at 03:56
  • 1
    Because you are creating a base class object from a derived class. No derived class information will be available. – dirkgently Mar 15 '10 at 07:44
  • I think in this case, no derived class information is avaiable is expected and a natual feature of C++. So, slicing is not a bad design? – George2 Mar 16 '10 at 13:50
  • Then why create an object of the derived class type? You should create and object of the base class. Now, you can argue, that this is not possible since your base class is abstract and then the real fun begins ... – dirkgently Mar 16 '10 at 14:36
1
struct A {
};

struct B : public A {
  int x;
};

void f( A a ) {
   // in here you can't access any of B's members - they have ben sliced
}

int main() {
    B b;
    f( b );   // slice!
}
1

Slicing is basically removing the derivedness of the derived class object to behave as if it were a base class object.

For ex:

class A
{
public:
   int x;
};

class B : public A
{
public:
   int y;
};

void test(A a)
{
}

int main()
{
 B b;
test(b); // here the object b will be visible as A object in method test
//none of the B's data or methods are available.
}
aJ.
  • 34,624
  • 22
  • 86
  • 128
  • 1
    @George2: Slicing is the natural result of C++ semantics, but it's usually a bug when it happens. – David Thornley Mar 12 '10 at 16:02
  • I think slicing means converting from derived class object to base class object. I think it is very common operation in C++, why you said it is a bug? – George2 Mar 15 '10 at 03:50