Is there a use for making a protected destructor virtual?

12,882

Solution 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 release).

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 ...)

Solution 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.

Solution 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.

Solution 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.

Share:
12,882
tusharfloyd
Author by

tusharfloyd

Updated on June 03, 2022

Comments

  • tusharfloyd
    tusharfloyd about 2 years
    /*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?

  • iammilind
    iammilind over 12 years
    Nice. And for that matter any other derived classes if depete pBase; attampted.
  • sharptooth
    sharptooth over 12 years
    No, you need a virtual destructor regardless of whether derived classes require any extra destruction, otherwise behavior is just undefined.
  • MartinStettner
    MartinStettner over 12 years
    @sharptooth Right, I didn't think of this. Fixed it, thanks for pointing it out!
  • bitmask
    bitmask over 12 years
    @user1085822: So, you're thanking me while unaccepting my answer. What are you trying to tell me?
  • Muxecoid
    Muxecoid over 11 years
    I saw some code that uses this trick to force all destruction to go through friend C-style wrapper function (defined per derived class). I guess the intent was similar but was lost under maintainence.
  • ksb
    ksb over 7 years
    Shouldn't this be just
  • ksb
    ksb over 7 years
    Shouldn't this be just - "protected: Base::~Base(); should be virtual at least if you (plan on) deleting any objects derived from Base within Base"? Why the "or a derived class of Base". part?
  • Alexandre Hamez
    Alexandre Hamez almost 6 years
    @MartinStettner See my answer: a protected destructor doesn't need to be virtual.
  • MaxNoe
    MaxNoe over 3 years
    libhdf5 uses a virtual protected destructor in H5Object. I don't know if that is a valid example or just a mistake though.
  • wmjdgla
    wmjdgla almost 2 years
    You missed another set of exceptions further down in the discussion section: "Some component architectures (e.g., COM and CORBA) don't use a standard deletion mechanism, and foster different protocols for object disposal. Follow the local patterns and idioms, and adapt this guideline as appropriate." MartinStettner's answer is basically describing COM and is thus acceptable under the Guidelines.
  • wmjdgla
    wmjdgla almost 2 years
    You need a virtual destructor even when you want to destroy a derived object through the derived class pointer. Deletion is implemented in the base class as delete this, so the type of this will be Base* even when doing Derived* foo = new Derived. So if the destructor is not virtual, only the Base class' destructor would be invoked.