If you delete an object, the destructor for that object is called, so you need to do the deletion in the destructor. Therefore, remember that everything that the class allocates to the heap must be freed in the destructor. If it was allocated on the stack, this happens automatically
struct A
{
A() { std::cout << "A()" << std::endl; ptr = new char[10]; }
~A() { std::cout << "~A()" << std::endl; delete []ptr; }
char *ptr;
};
, , A , , A , .
struct Base
{
virtual ~Base() {}
};
struct A : public Base
{
A() { std::cout << "A()" << std::endl; ptr = new char[10]; }
~A() { std::cout << "~A()" << std::endl; delete []ptr; }
char *ptr;
};