For example we have the following hierarchy.
class A
{
int a;
char* b;
public a() {}
};
class B
{
int c;
chard;
public:
}
Now, lets allocate memory for derivet class B, using the pointer type of the base class (let's using malloc, because this way is more understandable)
void* a = malloc(sizeof(A)); // we use the size of the class A, which is equal 8 byte in x32 machine
A* obj = new(a) B(); // placement new syntax runs the constructor of B()
The same we can write by following way
A* obj = new B()
In this case we allocate 8 byte of memory for class B and call the B's constructor, but B need to have 12 byte of size, what happens in this case ?
Aucun commentaire:
Enregistrer un commentaire