There are multiple questions pretty similiar to this one with the difference that there sizeof(Base) != sizeof(Derived). That won't work for obvious reasons (the subscript operator applied on the pointer is relative to the pointees size, not to the actual unterlying type). However, I was wondering whether this code would be correct or not:
struct Base
{
int Data;
};
struct Derived : public Base
{
};
int main()
{
static_assert(sizeof(Base) == sizeof(Derived), "Sizes are not equal");
Derived Data[10];
Base* Ptr = Data;
Ptr[3].Data = 5;
}
Obviously, Ptr[3] won't access any half ripped Base instances anymore since the sizes are equal, but is the code still correct?