12
/*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?

j0k
  • 22,303
  • 28
  • 77
  • 86
tusharfloyd
  • 1,372
  • 3
  • 11
  • 17
  • 3
    Maybe we should start with "why would you make dtor protected?". – Cat Plus Plus Jan 23 '12 at 10:53
  • 4
    Why did you ever want to make the destructor virtual? Shouldn't *you* know the purpose? A protected destructor means that objects shouldn't be destructed through base pointers, so the code in `main` is plain wrong. – thiton Jan 23 '12 at 10:53
  • See http://stackoverflow.com/questions/461203/when-to-use-virtual-destructors – user998692 Jan 23 '12 at 10:57
  • I understand the use of virtual dtors and the use of protected dtors but i saw some code with virtual protected dtors. What i did not understand was when both are used what effect will it creates? – tusharfloyd Jan 23 '12 at 11:06
  • @CatPlusPlus: by making dtors protected you will prevent creation of base class objects on stack. am i rite? – tusharfloyd Jan 23 '12 at 11:10

4 Answers4

20

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

Evg
  • 23,109
  • 5
  • 38
  • 74
MartinStettner
  • 28,001
  • 14
  • 77
  • 104
  • 3
    No, you need a virtual destructor regardless of whether derived classes require any extra destruction, otherwise behavior is just undefined. – sharptooth Jan 23 '12 at 11:14
  • @sharptooth Right, I didn't think of this. Fixed it, thanks for pointing it out! – MartinStettner Jan 23 '12 at 11:35
  • 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. – Muxecoid Nov 19 '12 at 14:35
  • 1
    @MartinStettner See my answer: a protected destructor doesn't need to be virtual. – Alexandre Hamez Aug 23 '18 at 11:30
13

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.

Alexandre Hamez
  • 7,263
  • 2
  • 29
  • 38
  • libhdf5 uses a virtual protected destructor in H5Object. I don't know if that is a valid example or just a mistake though. – MaxNoe Nov 28 '20 at 21:06
6

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

sharptooth
  • 163,328
  • 92
  • 501
  • 942
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.

bitmask
  • 27,748
  • 13
  • 86
  • 145
  • 1
    @user1085822: So, you're thanking me while unaccepting my answer. What are you trying to tell me? – bitmask Jan 23 '12 at 12:05
  • Shouldn't this be just – ksb Oct 04 '16 at 08:52
  • 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? – ksb Oct 04 '16 at 09:01