If we have the following code:
struct Base
{
int x;
int y;
void foo();
virtual unsigned getCrc() = 0;
};
struct Derived1 : public Base
{
int a;
int b;
unsigned getCrc();
};
struct Derived2 : public Base
{
float a;
float b;
unsigned getCrc();
};
Is it C++ standard that a
and b
should be after x
and y
in memory?
Or it is the most used method for laying out inherited objected? (i.e. compiler defacto-standard).
In other words, can I guarantee that:
Derived1 obj;
int* unsafe_internal_a = (int*)((unsigned)(&obj) + sizeof(Base));
EDIT: My question is 'Is memory layout covered in some standard? Or it is compiler dependent?'. The code is just for illustration.