Object slicing

Object slicing

In object-oriented programming, a subclass typically extends its superclass by defining additional member variables. If a superclass instance is assigned its value from a subclass instance, member variables defined in the subclass cannot be copied, since the superclass has no place to store them. This is a natural and unavoidable consequence of assignment by value from subclass objects. The term object slicing is sometimes used to refer to this aspect of assignment by value to a superclass instance.

Object slicing is also used to refer to a more subtle, problematic, case in which an object assignment by value appears to be to a superclass instance but is actually to a subclass instance. From the perspective of object memory layout, the member variables of the source instance can be thought of as having been "sliced off", leaving the corresponding member variables in the destination instance unchanged. It is this partial assignment (arguably a more apt term) that often surprises programmers and leads to unintended consequences.

Unexpected object slicing can happen in languages such as C++ in which assignment by value is not polymorphic. It is not possible in Java, which allows object assignment only by reference, or the D programming language, which allows object inheritance only through reference types.

For example, in C++:

struct A
{
    A(int a) : a_var(a) {}
    int a_var;
};
 
struct B : public A
{
    B(int a, int b) : A(a), b_var(b) {}
    int b_var;
};
 
B &getB()
{
    static B b(1, 2);
    return b;
}
 
int main()
{
    // Normal assignment by value to a
    A a(3);
    a = getB();
    // a.a_var == 1, b.b_var not copied to a
    //
    B b2(3, 4);
    A &a2 = b2;
    // Partial assignment by value through reference to b2
    a2 = getB();
    // b2.a_var == 1, b2.b_var == 4!
}

Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Slicing — may refer to: * Slicing, the mechanical process, see Cutting * Slicing (web design), a web design technique * Program slicing, a set of software engineering methods * Object slicing, an object oriented programming technique * Array slicing, an… …   Wikipedia

  • slicing — n. act of cutting into broad thin pieces; act of cutting across or cutting into slaɪs n. thin piece cut from a larger object; portion, share v. cut into slices, cut into portions; cut off from; make a cut in, cut into; cleave, move quickly… …   English contemporary dictionary

  • Array slicing — In computer programming, array slicing is an operation that extracts certain elements from an array and packages them as another array, possibly with different number of indices (or dimensions) and different index ranges. Two common examples are… …   Wikipedia

  • Slow slicing — Part of a series on Capital punishment Issues Debate · …   Wikipedia

  • List of object-oriented programming terms — Those words found in object oriented programming. Some are related to OOP and some not. A * Abstract class (also called deferred class) * Abstract method * Abstraction (computer science) * Access control * Accessor method * Allocated class *… …   Wikipedia

  • Vector (STL) — Vectors (or std::vector) are a C++ implementation of the dynamic array data structure. Their interface emulates the behavior of a C array (i.e., capable of fast random access) but with the additional ability to automatically resize itself when… …   Wikipedia

  • Cloning (programming) — In computer science, cloning refers to the making of an exact copy of an object, frequently under the paradigm of instance based programming, or object oriented programming(OOP). Shallow copies In most programming languages (exceptions include:… …   Wikipedia

  • Blade — For other uses, see Blade (disambiguation). Knife blades A blade is that portion of a tool, weapon, or machine with a cutting edge and/or a pointed tip that is designed to cut and/or puncture, stab, slash, chop, slice, thrust, or scrape animate… …   Wikipedia

  • Array data type — Not to be confused with Array data structure. In computer science, an array type is a data type that is meant to describe a collection of elements (values or variables), each selected by one or more indices that can be computed at run time by the …   Wikipedia

  • Ship of Theseus — Philosophy ( …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”