class has virtual functions and accessible non-virtual destructor

前端 未结 4 1270
-上瘾入骨i
-上瘾入骨i 2020-12-15 07:23

I have two classes:

class A {
public:
    virtual void somefunction() = 0;
};

class B : public A {
public:
    B();
    ~B();
    void somefunction();
};

B         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-15 08:09

    As thumb rule(IMHO) or in Short the destructor in the base class has to be either public and virtual or protected non virtual to prevent the memory leaks.by doing so the destructors of the derived class get called and this prevents the memory leakage whenever the Base pointer/reference holding derived address/reference is deleted.

提交回复
热议问题