To install click the Add extension button. That's it.

The source code for the WIKI 2 extension is being checked by specialists of the Mozilla Foundation, Google, and Apple. You could also do it yourself at any point in time.

4,5
Kelly Slayton
Congratulations on this excellent venture… what a great idea!
Alexander Grigorievskiy
I use WIKI 2 every day and almost forgot how the original Wikipedia looks like.
Live Statistics
English Articles
Improved in 24 Hours
Added in 24 Hours
What we do. Every page goes through several hundred of perfecting techniques; in live mode. Quite the same Wikipedia. Just better.
.
Leo
Newton
Brights
Milds

Object slicing

From Wikipedia, the free encyclopedia

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".

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.

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 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.

YouTube Encyclopedic

  • 1/3
    Views:
    1 959
    929
    5 906
  • What is Object Slicing in C++ Programming Language
  • What Is Object Slicing In C++?
  • Learn STL: Object Slicing

Transcription

Example

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;
}

See also

External links

This page was last edited on 19 January 2024, at 01:19
Basis of this page is in Wikipedia. Text is available under the CC BY-SA 3.0 Unported License. Non-text media are available under their specified licenses. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. WIKI 2 is an independent company and has no affiliation with Wikimedia Foundation.