Object Slicing¶
1. Problem Statement¶
Object slicing occurs when a derived object is copied or assigned into a base object by value. Only the base subobject is preserved; derived-state is discarded.
This is not an optimization detail. It is a direct consequence of value semantics in C++ object model.
Because slicing compiles cleanly and often runs “normally,” it is a high-risk correctness pitfall in polymorphic codebases.
2. Scope and Non-Goals¶
2.1 Scope¶
This article covers:
- why slicing happens,
- where it appears in real code,
- semantic and architectural consequences,
- robust API and design alternatives,
- review/migration checklists.
2.2 Non-Goals¶
We do not claim inheritance should be avoided entirely. The goal is to avoid value-semantic misuse in polymorphic hierarchies.
3. Minimal Example¶
struct Base {
virtual ~Base() = default;
virtual const char* name() const { return "Base"; }
};
struct Derived : Base {
const char* name() const override { return "Derived"; }
};
void print(Base b) { // slicing
// b is a standalone Base object
}
Passing a Derived object into print creates a new Base object.
Derived part is gone in b.
4. Why It Happens (Object Model View)¶
Derived contains a Base subobject plus additional state/behavior context.
By-value parameter Base b requires constructing a concrete Base object.
That construction copies only the base subobject. No dynamic type relationship is preserved in the copied object.
Polymorphism in C++ requires indirection (Base& / Base*),
not by-value base copies.
5. Static vs Dynamic Type Clarification¶
Before slicing:
- static type at call site may be
Base&orBase*, - dynamic type may be
Derived.
After slicing into Base value:
- static type is
Base, - dynamic type is also
Base.
Expecting derived virtual behavior after slicing is logically inconsistent.
6. Typical Manifestations¶
6.1 By-Value Parameters¶
6.2 By-Value Return Through Base¶
6.3 Container of Base Values¶
6.4 Assignment to Base Object¶
7. Semantic Consequences¶
Slicing can cause:
- loss of derived invariants/state,
- unexpected virtual dispatch outcomes,
- silently wrong business behavior,
- difficult-to-diagnose logic divergence.
Even without crashes, correctness can be compromised.
8. “Virtual Functions Don’t Save Sliced Objects”¶
Virtual dispatch relies on dynamic type of the object being called on.
After slicing, that object is a genuine Base.
So calling virtual functions on sliced Base value correctly dispatches to base implementations.
This is expected behavior, not compiler bug.
9. Safe Interface Pattern: References/Pointers¶
For polymorphic APIs, use:
const Base&for non-owning read access,Base&for mutable non-owning access,Base*/ smart pointers for nullable or owning paths.
Example:
10. Container Design: Avoid std::vector<Base> for Polymorphism¶
If elements are polymorphic, prefer owning indirection:
This preserves dynamic type and avoids slicing.
11. Ownership and Lifetime Considerations¶
Avoiding slicing often introduces pointer/reference usage. That shifts focus to ownership clarity.
Recommended:
- use
std::unique_ptr<Base>for unique ownership, - use
std::shared_ptr<Base>only for genuine shared lifetime, - keep raw pointers as non-owning observers.
12. Copy Semantics in Polymorphic Bases¶
Polymorphic bases copied by value are suspicious by default. Many designs should prohibit base copying:
struct Base {
Base() = default;
Base(const Base&) = delete;
Base& operator=(const Base&) = delete;
virtual ~Base() = default;
};
This blocks accidental slicing through copy paths.
13. Polymorphic Cloning Pattern¶
When deep-copy-like behavior is needed, use virtual clone interface:
#include <memory>
struct Base {
virtual ~Base() = default;
virtual std::unique_ptr<Base> clone() const = 0;
};
struct Derived : Base {
std::unique_ptr<Base> clone() const override {
return std::make_unique<Derived>(*this);
}
};
This preserves dynamic type without slicing.
14. Slicing in Constructors and Factories¶
Beware factory signatures returning Base by value.
Even if implementation constructs Derived,
result type forces slicing.
Prefer:
std::unique_ptr<Base>return,- or references managed by clear owner.
15. Hidden Slicing in Utility Functions¶
Common subtle case:
Template forwarding around this can hide slicing at call sites. APIs should avoid by-value base parameters entirely in polymorphic paths.
16. Logging/Debug Code Also Needs Discipline¶
Slicing often enters code via “harmless” logging helpers:
Such helpers may become correctness-critical over time. Use reference-based signatures consistently.
17. Performance Myths¶
Some developers pass base by value to “avoid pointer indirection.” In polymorphic design this is usually incorrect trade-off, because it destroys semantic intent.
Performance optimization should not invalidate object-model contracts.
18. Tooling and Diagnostics¶
Enable warnings/analysis that detect slicing opportunities.
Useful checks:
- pass-by-value of polymorphic base,
- insertion of derived into base-valued containers,
- suspicious copy constructors in polymorphic hierarchies.
Tooling helps, but design review is primary defense.
19. Common Anti-Patterns¶
- Polymorphic API accepting
Baseby value. - Returning
Baseby value from virtual/factory abstractions. - Storing polymorphic objects in
std::vector<Base>. - Assuming virtual dispatch still reaches derived after slicing.
- Mixing value-semantics assumptions with runtime-polymorphism goals.
20. Migration Strategy for Existing Code¶
- Search for by-value parameters/returns of polymorphic bases.
- Replace with references/pointers/smart pointers based on ownership.
- Refactor base-value containers to owning polymorphic containers.
- Add tests verifying derived behavior survives through API layers.
- Consider deleting copy operations on polymorphic bases.
21. Review Checklist¶
- [ ] Is any polymorphic base passed/returned by value?
- [ ] Are polymorphic objects stored in base-value containers?
- [ ] Is ownership explicit where references/pointers are introduced?
- [ ] Are clone/copy semantics explicit for polymorphic duplication?
- [ ] Are warnings for slicing enabled in CI/tooling?
- [ ] Do tests assert dynamic behavior, not just static compilation?
If any answer is uncertain, slicing risk remains.
22. Safer Form Summary¶
- Use
const Base&/Base&/Base*for polymorphic APIs. - Use smart pointers for ownership-aware polymorphic storage/returns.
- Avoid base-by-value interfaces in polymorphic designs.
- Explicitly design cloning when polymorphic copy is required.
- Treat slicing as semantic bug, not cosmetic issue.
Example¶
A runnable version of this topic:
23. Conclusion¶
Object slicing is a direct consequence of value semantics applied to inheritance hierarchies. It is predictable, silent, and frequently harmful.
The reliable engineering rule is simple:
runtime polymorphism requires indirection; base-by-value APIs are incompatible with preserving derived behavior.
When teams enforce this consistently, an entire class of subtle polymorphism bugs can be eliminated early in review.