I don't understand one thing. For example, I declare class A and class B which is a child of A:
class A {
public:
int a;
}
class B : public A {
public:
int b;
}
Obviously, if I create instances of A or B, their size in the memory can be determined by the type.
A instanceA; // size of this will probably be the size of int (property a)
B instanceB; // size of this will probably be twice the size of int (properties a and b)
But what if I create dynamic instances and then free them later?
A * instanceAPointer = new A();
A * instanceBPointer = new B();
These are instances of different classes but the program will consider them as instances of class A. That's fine while using them but what about freeing them? To free allocated memory, the program must know the size of memory to free, right?
So if I write
delete instanceAPointer;
delete isntanceBPointer;
How does the program know, how much memory, starting from the address each pointer is pointing to, it should free? Because obviously the objects have different size but the program considers them to be of type A.
Thanks