Subscribe For Free Updates!

We'll not spam mate! We promise.

Apr 14, 2013

How do I free the memory allocated to a class?



How do I free the memory allocated to a class?

The method called when the memory occupied by a class is freed is called the destruc-
tor. With derived classes, destructor should always be virtual. If a class is destroyed
through a base class pointer whose destructor isn't virtual, the result is unde ned
(only part of the destructors will be called).

struct A
{
A() {}
virtual ~A() {}
};
struct B: public A
{
B() {}
~B() {}
};
void function()
{
B *b = new B();
A *a = b;
delete a; // calls ~A and ~B. If ~A wasn't virtual, only ~A would be called.
}

TechniqZone Socializer Widget
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment