一句话:避免内存泄露。
示例代码如下:
class Base
{
public:
Base(){m_p1=new int(1);}
~Base(){delete m_p1;}
protected:
int *m_p1;
};
class Derive:public Base
{
public:
Derive(){m_p2=new int(2);}
~Derive(){delete m_p2;}
private:
int *m_p2;
};
int main()
{
Base* pDerive=new Derive;
delete pDerive;
return 0;
}
上面的程序将产生内存泄露,因为只会调用Base的析构函数,而不会调用Derive的析构函数。