Object Slicing

A limitation of some OO languages, typically StaticallyTyped languages in which objects can be passed by value, such as CeePlusPlus, in which information about an object is lost by converting it to a static value type different from (usually a superclass of) its run-time type. Conceptually similar to the 'C' idea of casting.

Never, ever a good thing. In C++, this will slice:

  void function1() {
      mysubclass someObject;// Where mysubclass   is derived
                                        // from  mysuperclass
      // the cast will use the copy-constructor of mysuperclass, which will not
      // preserve the virtual table (the superclass instance operated on by f2
      // is not the same instance as the one which is part of someObject)
      function2(someObject);
  }

void function2(mysuperclass inObject) inObject.someOperation();// Runs the superclass version // regardless - which is wrong! }

This will not slice:

  void function1() {
      mysubclass someObject;// Where mysubclass   is derived 
                                        // from  mysuperclass
      // This time, the object is passed by reference, and the superclass
      // instance used is the same instance as that which is part of someObject
      function2(&someObject);
  }

void function2(mysuperclass *inObject) { inObject->someOperation(); }


Wouldn't the above be clearer if you used the following as the non-slicing example:

  void function1() {
      mysubclass someObject;// Where mysubclass   is derived
                                        // from  mysuperclass
      function2(someObject);
  }

void function2(mysuperclass& inObject) { inObject.someOperation();// Runs the real version because it // still knows what the object type is! }


Some people are more confident with references than with pointers. Apart from syntax, they are both the same thing.


This depends on the OO language that you are using. For example, Java does not have this problem, since Java only passes objects by reference. This is typically in C++. Also note that you have to declare the function as 'virtual'.


Java passes references by value, not objects by reference as you say. They are very different things. Passing by reference allows you to swap objects between two variables; Java won't let you do this at all. Try it. Pass two objects to a function and try to swap them with a temp variable --- after the function returns, the two objects will be in the same place.


Java passes references by value, not objects by reference as you say. They are very different things

They are the same thing. The reason you can't swap them is simply that Java has no facility to replace the pointed object with a different one. In C, with the same pointer you could replace the pointed value by copying a new one in its place. It would be even easier if the reference was passed by reference, then you could swap the object with an object of different layout as well.

I do not agree that object slicing is never a good thing. If the intention is explicitly to deal with the object as a mysuperclass copy, then you should be able to. The problem is that the code above does not convey the idea clearly.


CategoryJargon


EditText of this page (last edited October 18, 2010) or FindPage with title or text search