I was wondering if I can't just ignore object slicing in a situation similar to the following:
class Base
{
private:
int8_t data[128];
// other variables
protected:
Base(args) : args(args) { }
void setData(uint8_t i, int8_t d) { data[i] = d; }
public:
void doSomethingWithData() { ... }
}
class Derived
{
public:
Derived(args) : Base(args)
{
setData(...);
setData(...);
}
}
Base array[] = {
Derived(args),
Derived2(args)
..
}
Base& any = array[0];
According to what I thought about object slicing, even if Derived
specific copy/move constructor/assignment operators are lost, there is nothing to lose here so it could be a safe operation, assuming that any Derived
limits itself to calling the Base
constructor and setting some data. But this could be a typical pitfall that I'm overlooking.
Any guaranteed behavior about it?