<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki1463/index.php?action=history&amp;feed=atom&amp;title=Object_slicing</id>
	<title>Object slicing - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki1463/index.php?action=history&amp;feed=atom&amp;title=Object_slicing"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki1463/index.php?title=Object_slicing&amp;action=history"/>
	<updated>2026-12-04T02:25:55Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.46.3</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki1463/index.php?title=Object_slicing&amp;diff=138970&amp;oldid=prev</id>
		<title>~2026-41267-59 at 03:13, 25 July 2026</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki1463/index.php?title=Object_slicing&amp;diff=138970&amp;oldid=prev"/>
		<updated>2026-07-25T03:13:56Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Problem of data truncation in polymorphic objects}}&lt;br /&gt;
{{About|object-oriented programming|other uses of slicing|Slicing (disambiguation)}}&lt;br /&gt;
{{More citations needed|date=March 2025}}&lt;br /&gt;
&lt;br /&gt;
In [[C++]] programming, &amp;#039;&amp;#039;&amp;#039;object slicing&amp;#039;&amp;#039;&amp;#039; occurs when an [[Object-oriented programming|object]] of a [[Subtyping|subclass type]] is copied to an object of superclass type: the superclass copy will not have any of the [[member variable]]s or [[Method (computer programming)#Member functions in C++|member functions]] defined in the subclass. These variables and functions have, in effect, been &amp;quot;sliced off&amp;quot;.&amp;lt;ref name=&amp;quot;l760&amp;quot;&amp;gt;{{cite book | last=R. | first=Subburaj | title=Object Oriented Programming with C++ ANSI /ISO Standard | publisher=Vikas Publishing House | date=2013 | isbn=978-93-259-6996-4 | page=260-261}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;q306&amp;quot;&amp;gt;{{cite book | last=Grimes | first=Richard | title=Beginning C++ Programming | publisher=Packt Publishing Ltd | publication-place=Birmingham | date=2017-04-24 | isbn=978-1-78712-928-3 | page=309}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More subtly, object slicing can likewise occur when an object of a subclass type is copied to an object of the &amp;#039;&amp;#039;same&amp;#039;&amp;#039; type by the superclass&amp;#039;s [[assignment operator]], in which case some of the target object&amp;#039;s member variables will retain their original values instead of getting copied over from the source object.&lt;br /&gt;
&lt;br /&gt;
This issue is not inherently unique to C++, but it does not occur naturally in most other object-oriented languages — not even in C++&amp;#039;s relatives such as [[D (programming language)|D]], [[Java (programming language)|Java]], and [[C Sharp (programming language)|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 &amp;#039;&amp;#039;reference&amp;#039;&amp;#039; 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 (programming language)|Rust]] avoids object slicing entirely by avoiding structural [[Inheritance (object-oriented programming)|inheritance]], while using [[Trait (computer programming)|traits]] and [[dynamic dispatch]] to achieve polymorphism and enforcing type size safety using the &amp;lt;code&amp;gt;std::marker::Sized&amp;lt;/code&amp;gt; trait.&amp;lt;ref&amp;gt;{{Cite web|title=Trait Sized|url=https://doc.rust-lang.org/std/marker/trait.Sized.html|author=The Rust Devs|publisher=The Rust Devs|website=doc.rust-lang.org|date=14 July 2026}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Additionally, due to the lack of [[garbage collection (computing)|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 {{code|std::vector}}) typically involves making and inserting a &amp;#039;&amp;#039;copy&amp;#039;&amp;#039; into the collection.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Object slicing has been the target of criticism for its subtleties.&amp;lt;ref&amp;gt;{{Cite web|title=25.9 - Object Slicing|url=https://www.learncpp.com/cpp-tutorial/object-slicing/|website=learncpp.com|author=Alex Pomeranz|publisher=LearnCpp.Com|date=28 October 2024}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class Base {&lt;br /&gt;
private:&lt;br /&gt;
    int a;&lt;br /&gt;
public:&lt;br /&gt;
    explicit Base(int a): &lt;br /&gt;
        a{a} {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived : public Base {&lt;br /&gt;
private:&lt;br /&gt;
    int b;&lt;br /&gt;
public:&lt;br /&gt;
    Derived(int a, int b): &lt;br /&gt;
        Base{a}, b{b} {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
Derived&amp;amp; getDerived() {&lt;br /&gt;
    static Derived derived(1, 2);&lt;br /&gt;
    return derived;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    // Normal assignment by value to a&lt;br /&gt;
    Base base(3);&lt;br /&gt;
    // base.a == 3&lt;br /&gt;
    base = getDerived();&lt;br /&gt;
    // base.a == 1, derived.b not copied to base&lt;br /&gt;
&lt;br /&gt;
    Derived derived(3, 4);&lt;br /&gt;
    // derived.a == 3, derived.b == 4&lt;br /&gt;
    Base&amp;amp; base2 = derived;&lt;br /&gt;
    // Partial assignment by value through reference to derived&lt;br /&gt;
    base2 = getDerived();&lt;br /&gt;
    // derived.a == 1, derived.b == 4 !&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Diamond problem]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
{{C++ programming language}}&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:Object Slicing}}&lt;br /&gt;
[[Category:Object-oriented programming]]&lt;br /&gt;
[[Category:Articles with example C++ code]]&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>~2026-41267-59</name></author>
	</entry>
</feed>