[C++] virtual destructor behavior

From: Spacen Jasset (spacenjasset_at_fastmail.fm)
Date: 02/29/04


Date: Sun, 29 Feb 2004 11:31:26 -0000

I have this scenario:

class BASE
{
    // virtual ~BASE() {}
};

class SUB : public BASE
{
    list<char*> patterns;
    ~SUB()
    {
        cout << "~SUB()" << endl;
    }
};

Then I say:

SUB *s = //something;
delete s;

It seems that the destructor for patterns in SUB doesn't get called, but
~SUB() does get called. This is not what I expected. Giving BASE a virtual
destructor fixes this. I would expect this not to work if 's' had the static
type BASE, but it does not. it's static type is SUB.



Relevant Pages