C++ Reference ยท Types and Language Concepts
Object slicing in C++
Reference for C++ object slicing: what gets lost when a derived object is copied into a base object, why virtual calls stop working, and how to avoid it.
Object slicing happens when you copy a derived-class object into a variable of its base-class type. The base class copy constructor (or copy assignment operator) only knows about the base part, so everything the derived class added is cut off. What remains is a genuine base object, not a derived object seen through a base view.
Slicing is silent: it compiles without any warning in the usual cases. The typical symptoms are lost data members and virtual calls that run the base version instead of the override. It only happens with values. References and pointers to the base never slice anything.
Derived d; Base b = d; // slicing: only the Base part is copied Base& r = d; // no slicing: r refers to the whole object
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
Slicing has no syntax of its own; it is the result of these forms, where Derived publicly inherits from Base:
Base b1 = derived; // copy initialization: slices Base b2(derived); // direct initialization: slices b1 = derived; // assignment: slices void f(Base b); // f(derived) slices the argument std::vector<Base> v; // v.push_back(derived) slices
| Form | Sliced? |
|---|---|
Base b = derived; |
Yes, a new Base object is created from the base subobject. |
Base& r = derived; / Base* p = &derived; |
No, these refer to the original object. |
void f(Base b) called with a derived object |
Yes. |
void f(const Base& b) |
No. |
std::vector<Base> and std::array<Base, N> |
Yes, they hold Base values. |
std::vector<std::unique_ptr<Base>> |
No. |
Usage notes
- Data is lost. Members declared in
Deriveddo not exist in theBasecopy. You cannot get them back with a cast, because the object is no longer aDerived. - Virtual calls stop being polymorphic. The copy has the dynamic type
Base, sospeak()runsAnimal::speak, as the first example shows. - Assignment can slice an existing object.
Base& a = d1; Base& b = d2; a = b;callsBase::operator=and copies only the base parts. The two objects can end up inconsistent (the base part ofd1comes fromd2, the derived part stays). - Prevent it by design. Make the base class abstract (a pure virtual function makes by-value copies of the base impossible), or delete the copy operations in the base and offer a virtual
clone()function instead. - Protect against it in the base:
Base(const Base&) = delete;stops accidental copies, but then derived classes need their own copy operations, so a protected defaulted copy constructor is a common compromise. - Pass polymorphic objects by reference or pointer. Use
const Base&for read-only access and smart pointers for ownership. - Not an error for plain data. Copying a derived object into a base when you really only want the base part (for example, converting
EmployeetoPersonon purpose) is fine, but make it visible in the code. - Exceptions are affected too.
catch (Base e)slices the thrown object. Catch byconstreference.