Alex Rivera | Logout

What is object slicing?

Asked 2008-11-08T11:10:13.897
934

In C++, what is object slicing and when does it occur?

Edit
Report

1 Answer

34

The slicing problem is serious because it can result in memory corruption, and it is very difficult to guarantee a program does not suffer from it. To design it out of the language, classes that support inheritance should be accessible by reference only (not by value). The D programming language has this property.

Consider class A, and class B derived from A. Memory corruption can happen if the A part has a pointer p, and a B instance that points p to B's additional data. Then, when the additional data gets sliced off, p is pointing to garbage.

answered 2008-11-08T11:56:54.410

Your Answer