MarketObject slicing
Company Profile

Object slicing

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

Example
class Base { private: int a; public: explicit Base(int a): a{a} {} }; class Derived : public Base { private: int b; public: explicit Derived(int a, int b): Base{a}, b{b} {} }; Derived& getDerived() { static Derived derived(1, 2); return derived; } int main() { // Normal assignment by value to a Base base(3); // base.a == 3 base = getDerived(); // base.a == 1, derived.b not copied to base Derived derived(3, 4); // derived.a == 3, derived.b == 4 Base& base2 = derived; // Partial assignment by value through reference to derived base2 = getDerived(); // derived.a == 1, derived.b == 4 ! return 0; } ==See also==
tickerdossier.comtickerdossier.substack.com