C++ tidbit #6: Virtual functions and trivial copy
Trivial Copyability
In C++ terms an object is `trivially-copyable` if it is ok to memcpy it around. Containers (stl and custom) typically rely on `std::is_trivially_copyable<T>` to specialize copy operations of a bulk of T objects. Techincally, A copy ctor is considered trivial when :
Condition (1) seems reasonable, but why condition (2)? What might go wrong when memcpy'ing an instance of a class with virtual functions?
Object Slicing
Let's focus on this toy snippet.
领英推荐
struct B {
? ? int b;
? ? virtual void f();
};
struct D : B {
? ? int d;
? ? void f() override;
};
Everything is fine if you memcpy real B's around, and everything is fine if you memcpy real D's.
But what about -
D d;
B& b = d; // can we memcpy?
This is essentially object slicing: assigning a derived object to a parent (and in the process, 'slicing' away the derived-only members). Our code is more referencing than assigning, but the essential problem remains if we try to memcpy:
The 1st element of the object's memory is a pointer to the class' vtable (a table of pointers to the virtual functions, shared among all class instances). A few things to note: