// Workers AI · dad joke modeWhat did object slicing say to itself? "I'm a cut above.
This article needs more citations. (March 2025) |
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. Meanwhile, Rust avoids object slicing entirely by avoiding structural inheritance, while using traits and dynamic dispatch to achieve polymorphism and enforcing type size safety using the std::marker::Sized trait.[3]
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.[4]
Example
[edit source]class Base {
private:
int a;
public:
explicit Base(int a):
a{a} {}
};
class Derived : public Base {
private:
int b;
public:
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 source]References
[edit source]- ↑ R., Subburaj (2013). Object Oriented Programming with C++ ANSI /ISO Standard. Vikas Publishing House. p. 260-261. ISBN 978-93-259-6996-4.
- ↑ Grimes, Richard (2017-04-24). Beginning C++ Programming. Birmingham: Packt Publishing Ltd. p. 309. ISBN 978-1-78712-928-3.
- ↑ The Rust Devs (14 July 2026). "Trait Sized". doc.rust-lang.org. The Rust Devs.
- ↑ Alex Pomeranz (28 October 2024). "25.9 - Object Slicing". learncpp.com. LearnCpp.Com.