Is there a use for making a protected destructor virtual?

只愿长相守 提交于 2019-12-30 08:08:05

问题


/*Child is inherited from Parent*/
class Parent {  
  public:  
    Parent () //Constructor
    {
        cout << "\n Parent constructor called\n" << endl;
    }
  protected:
    ~Parent() //Dtor
    {
        cout << "\n Parent destructor called\n" << endl;
    }
};

class Child : public Parent 
{
  public:
    Child () //Ctor
    {
        cout << "\nChild constructor called\n" << endl;
    }
    ~Child() //dtor
    {
        cout << "\nChild destructor called\n" << endl;
    }
};

int main ()
{
    Parent * p2 = new Child;          
    delete p2;
    return 0;
}

If I make Parent's destructor virtual, then I obtain an error, so what is the purpose of making a protected destructor virtual?


回答1:


Just to give one example: Say you have an base class which implements reference counting. You have an addRef and a release method and you want your object to be destroyed, if (and only if) the internal counter reaches zero through a call to release.

So, first you want your destructor protected (since you only want to destroy the object from within relase).

If you plan to derive from your class, you also want to have your destructor virtual, since you need a virtual destructor whenever you want to destroy a child object through a pointer to a base class (thanks @sharptooth for the hint ...)




回答2:


There's an entry in the C++ Core Guidelines dedicated to this specific subject:

C.35: A base class destructor should be either public and virtual, or protected and nonvirtual

Reason To prevent undefined behavior. If the destructor is public, then calling code can attempt to destroy a derived class object through a base class pointer, and the result is undefined if the base class’s destructor is non-virtual. If the destructor is protected, then calling code cannot destroy through a base class pointer and the destructor does not need to be virtual; it does need to be protected, not private, so that derived destructors can invoke it. In general, the writer of a base class does not know the appropriate action to be done upon destruction.

So, the destructor doesn't need to be virtual if it's protected. However, there is an exception:

Exception We can imagine one case where you could want a protected virtual destructor: When an object of a derived type (and only of such a type) should be allowed to destroy another object (not itself) through a pointer to base. We haven’t seen such a case in practice, though.

So, to sum up, in practice a protected destructor does not need to be virtual.




回答3:


Yes, if you intend to do delete this in class Parent member functions which is very common when implementing IUnknown::Release() in COM objects.




回答4:


protected: Base::~Base(); should be virtual at least if you (plan on) deleting any objects derived from Base within Base or a derived class of Base.



来源:https://stackoverflow.com/questions/8970466/is-there-a-use-for-making-a-protected-destructor-virtual

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!