Why is my destructor never called?

后端 未结 4 1466
温柔的废话
温柔的废话 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:10

    try this:

    class A
    {
    public:
        virtual ~A() {}
        virtual f();
    };
    
    class B : public A
    {
    public:
         B()
         {
             p = new char [100];
         }
         virtual ~B() // virtual keywork optional but occasionally helpful for self documentation.
         {
             delete [] p;
         }
         f();
    private:
        char *p;
    };
    

提交回复
热议问题