Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Object slicing
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{About|object-oriented programming|other uses of slicing|Slicing (disambiguation)}} {{More citations needed|date=March 2025}} In [[C++]] programming, '''object slicing''' 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 "sliced off".<ref name="l760">{{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}}</ref><ref name="q306">{{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}}</ref> 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 (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 ''reference'' is a basic operation. In C++, by contrast, objects are copied automatically whenever a function takes an object argument by value or returns an object by value. 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 {{mono|std::vector}}) typically involves making and inserting a ''copy'' into the collection. ==Example== <syntaxhighlight lang="cpp"> struct A { A(int a) : a_var(a) {} int a_var; }; struct B : public A { B(int a, int b) : A(a), b_var(b) {} int b_var; }; B &getB() { static B b(1, 2); return b; } int main() { // Normal assignment by value to a A a(3); // a.a_var == 3 a = getB(); // a.a_var == 1, b.b_var not copied to a B b2(3, 4); // b2.a_var == 3, b2.b_var == 4 A &a2 = b2; // Partial assignment by value through reference to b2 a2 = getB(); // b2.a_var == 1, b2.b_var == 4! return 0; } </syntaxhighlight> ==References== {{Reflist}} ==See also== * [[Diamond problem]] {{C++ programming language}} {{DEFAULTSORT:Object Slicing}} [[Category:Object-oriented programming]] [[Category:Articles with example C++ code]] [[Category:C++]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:About
(
edit
)
Template:C++ programming language
(
edit
)
Template:Cite book
(
edit
)
Template:Mono
(
edit
)
Template:More citations needed
(
edit
)
Template:Reflist
(
edit
)