A pointer to a derived class implicitly converts to a pointer to its public base class. When applied to a copy operation, this simple and necessary rule leads to a trap for the unwary. Consider:
struct Base {
int b;
Base(const Base&);
// ...
};
struct Derived : Base
{
int d;
Derived(const Derived&);
// ...
};
void naive(Base∗ p)
{ Bb2=∗p; //mayslice: invokes Base::Base(const Base&)
// ...
}
void user()
{
Derived d;
naive(&d);
Base bb = d; // slices: invokes Base::Base(const Base&), not Derived::Der ived(const Derived&)
// ...
}
The above para is from The C++ Programming Language book, 4th edition. Now why this problem only to copy operations. Doesn't this issue occur for move operations?