| 1 |
.. title:: clang-tidy - cppcoreguidelines-slicing |
| 2 |
|
| 3 |
cppcoreguidelines-slicing |
| 4 |
========================= |
| 5 |
|
| 6 |
Flags slicing of member variables or vtable. Slicing happens when copying a |
| 7 |
derived object into a base object: the members of the derived object (both |
| 8 |
member variables and virtual member functions) will be discarded. This can be |
| 9 |
misleading especially for member function slicing, for example: |
| 10 |
|
| 11 |
.. code-block:: c++ |
| 12 |
|
| 13 |
struct B { int a; virtual int f(); }; |
| 14 |
struct D : B { int b; int f() override; }; |
| 15 |
|
| 16 |
void use(B b) { // Missing reference, intended? |
| 17 |
b.f(); // Calls B::f. |
| 18 |
} |
| 19 |
|
| 20 |
D d; |
| 21 |
use(d); // Slice. |
| 22 |
|
| 23 |
See the relevant C++ Core Guidelines sections for details: |
| 24 |
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es63-dont-slice |
| 25 |
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c145-access-polymorphic-objects-through-pointers-and-references |