-1

When I tried to assign a derived class object to a base class, the 'unique' part in the derived class will be sliced down. So what happen to those part that being sliced away? Are they destroyed properly or they are just lost in memory? And if I alloc some memories on those derived part, when the object slicing happen, will the alloced memories be freed properly? I suppose it will lead to some undefined behavior, since we never declare an approach to destroyed those derived parts when object slicing.

And after all, what's a situation that we need to use object slicing? Or we should always avoid that ?

Ziqi Liu
  • 2,931
  • 5
  • 31
  • 64
  • Depends on how you do it. When you assign the source is untouched, the bits that the base doesn't know about simply don't get copied. – user4581301 Sep 28 '18 at 04:20
  • *Or we should always avoid that ?* [Is object slicing ever useful?](https://stackoverflow.com/questions/16416164/is-object-slicing-ever-useful) – user4581301 Sep 28 '18 at 04:23

1 Answers1

-1

Object slicing generally occurs when you are copying derive class object into the base class object. So the base class object have the data whatever left after slicing. The other part in the base object gets lost. To avoid it you can use pointers or references to objects are passed as function arguments since a pointer or reference of any type takes same amount of memory. Alternatively, Object slicing can be prevented by making the base class function pure virtual there by disallowing object creation. It is not possible to create the object of a class which contains a pure virtual method.

#include <iostream> 
using namespace std; 

class Base 
{ 
protected: 
    int i; 
public: 
    Base(int a)     { i = a; } 
    virtual void display() 
    { cout << "I am Base class object, i = " << i << endl; } 
}; 

class Derived : public Base 
{ 
    int j; 
public: 
    Derived(int a, int b) : Base(a) { j = b; } 
    virtual void display() 
    { cout << "I am Derived class object, i = "
           << i << ", j = " << j << endl;  } 
}; 

// Global method, Base class object is passed by value 
void somefunc (Base obj) 
{ 
    obj.display(); 
} 

int main() 
{ 
    Base b(33); 
    Derived d(45, 54); 
    somefunc(b); 
    somefunc(d);  // Object Slicing, the member j of d is sliced off 
    return 0; 
} 

We can avoid above unexpected behavior with the use of pointers or references. Object slicing doesn’t occur when pointers or references to objects are passed as function arguments since a pointer or reference of any type takes same amount of memory. For example, if we change the global method myfunc() in the above program to following, object slicing doesn’t happen.

// rest of code is similar to above 
void somefunc (Base &obj) 
{ 
    obj.display(); 
}            
// rest of code is similar to above 

We get the same output if we use pointers and change the program to following.

// rest of code is similar to above 
void somefunc (Base *objp) 
{ 
    objp->display(); 
} 

int main() 
{ 
    Base *bp = new Base(33) ; 
    Derived *dp = new Derived(45, 54); 
    somefunc(bp); 
    somefunc(dp);  // No Object Slicing 
    return 0; 
} 
sonulohani
  • 1,225
  • 9
  • 8