Jump to content

Object slicing

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 2620:0:1000:5b10:7721:3413:4dd5:9b03 (talk) at 18:50, 10 May 2018 (The point of the article is that some values *don't* change due to object slicing, but the comment has all the values changing. b_var shouldn't change, since the A copy constructor doesn't know about it.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In C++ programming, object slicing occurs when an object of a subclass type is copied to an object of superclass type: the superclass copy will not have any of the member variables defined in the subclass. (These variables have, in effect, been "sliced off".) More subtly, object slicing can also occur when an object of a subclass type is copied to an object of the same type by the superclass's assignment operator, in which case some of the target object's member variables will retain their original values instead of being copied from the source object.

This issue is not inherently unique to C++, but it does not occur naturally in most other object-oriented languages — even C++'s relatives such as D, Java, and C# — because copying of objects is not a basic operation in those languages. (Instead, those languages prefer to manipulate objects via implicit references, such that only copying the reference is a basic operation.) In C++, by contrast, objects are copied automatically whenever a function takes an object argument by value or returns an object by value. Additionally, due to the lack of garbage collection in C++, programs will frequently copy an object whenever the ownership and lifetime of a single shared object would be unclear; for example, inserting an object into a standard-library collection, such as a std::vector, actually involves inserting a copy into the collection.

Example

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.a_var ==3
    a = getB();
    // a.a_var == 1, b.b_var not copied to a

    B b2(3, 4);
//b2.a_var==3 ,b2.b_var==4
    A &a2 = b2;
    // Partial assignment by value through reference to b2
    a2 = getB();
    // b2.a_var == 1, b2.b_var == 4!

    return 0;
}

See also