Why is my destructor never called?

后端 未结 4 1371
温柔的废话
温柔的废话 2021-02-20 01:07

I have a base class A and a derived class B:

class A
{
public:
    virtual f();
};

class B : public A
{
public:
     B()
     {
         p = new char [100];
            


        
4条回答
  •  梦谈多话
    2021-02-20 01:22

    If your variable is of type A it doesn't have a virtual destructor and so it won't look at the actual runtime type of the object to determine it needs to call the desstructor

    Add an empty destructor to A

    virtual ~A() {}

    and that should fix it.

    In general you need to do this on any class that can possibly be used as a base class.

提交回复
热议问题