I do not understand anything. 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 memory can be determined by 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 release 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. This is great when using them, but how to free them? To free allocated memory, a program must know the size of free memory, right?
So if I write
delete instanceAPointer; delete isntanceBPointer;
As the program knows, how much memory, starting from the address pointed to by the pointer, should it free? Because, obviously, the objects are of different sizes, but the program considers them to be of type A.
thanks
source share