📑 Table of Contents

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 or member functions defined in the subclass. These variables and functions have, in effect, been "sliced off".[1][2]

More subtly, object slicing can likewise 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 getting copied over from the source object.

This issue is not inherently unique to C++, but it does not occur naturally in most other object-oriented languages — not even in 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. By contrast, in C++ objects are copied automatically whenever a function takes an object argument by value or returns an object by value. Similarly, this does not happen in Rust as Rust lacks inheritance.

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) typically involves making and inserting a copy into the collection.

When using method chaining and the fluent interface pattern, there is a potential risk of object slicing in C++ if a method from a base class is called on a derived class that returns a reference to the base class.

Object slicing has been the target of criticism for its subtleties.[3]

Example

edit
class Base {
private:
    int a;
public:
    explicit Base(int a): 
        a{a} {}
};

class Derived : public Base {
private:
    int b;
public:
    explicit Derived(int a, int b): 
        Base{a}, b{b} {}
};

Derived& getDerived() {
    static Derived derived(1, 2);
    return derived;
}

int main() {
    // Normal assignment by value to a
    Base base(3);
    // base.a == 3
    base = getDerived();
    // base.a == 1, derived.b not copied to base

    Derived derived(3, 4);
    // derived.a == 3, derived.b == 4
    Base& base2 = derived;
    // Partial assignment by value through reference to derived
    base2 = getDerived();
    // derived.a == 1, derived.b == 4 !

    return 0;
}

See also

edit

References

edit
  1. ^ R., Subburaj (2013). Object Oriented Programming with C++ ANSI /ISO Standard. Vikas Publishing House. p. 260-261. ISBN 978-93-259-6996-4.
  2. ^ Grimes, Richard (2017-04-24). Beginning C++ Programming. Birmingham: Packt Publishing Ltd. p. 309. ISBN 978-1-78712-928-3.
  3. ^ Alex Pomeranz (28 October 2024). "25.9 - Object Slicing". learncpp.com. LearnCpp.Com.

📚 Artikel Terkait di Wikipedia

Slicing

Look up slicing in Wiktionary, the free dictionary. Slicing may refer to: Array slicing, an operation on an array in computer science Object slicing, an object-oriented

Method chaining

*this). However, in C++ it is important to be conscious of potential object slicing if the returned reference is to a parent class, while other languages

Slic3r

Free and open-source software portal Slic3r is free software 3D slicing engine for 3D printers. It generates G-code from 3D CAD files (STL or OBJ). Once

Slicer (3D printing)

A slicer is a toolpath generation software used in 3D printing. It facilitates the conversion of a 3D object model to specific instructions for the printer

Multi-material 3D printing

multiple material object. However, not all file formats support the annotation of materials together with the geometry of the object. Slicing is the process

False color

see. In addition, variants of false colors such as pseudocolors, density slicing, and choropleths are used for information visualization of either data

Value type and reference type

non-references can lead to problems such as object slicing, at least when inheritance is used; in languages where objects belong to reference types, these problems

Array slicing

to the very last item. The concept of slicing was surely known even before the invention of compilers. Slicing as a language feature probably started