Why the destructor of base class TH1 and TH2 not virtual?

Dear experts

I want to declare a container that arrange the memory of histograms std:vector<TH1*> and pointing to different sub objects (like TH1D and TH1F), but when I need to delete all of them I found the base class has a non-virtual destructor. Is there any reason that we make it non-virtual?

I’m not a expert of C++ so sorry if this question is basic!

Which base class has a non-virtual destructor?

The TH1D class inherits from TH1 and TArrayD. The TArrayD has a virtual destructor and C++, if a base class has a virtual destructor, then the destructors in derived classes are also virtual, even if they are not explicitly marked as virtual .
The same case is for TH1F and TArrayF, etc.

Thank you for your reply, the base class I meant is TH1 class, and I’m wondering if there is memory leak when I use TH1* h = new TH1D(); then delete h;

TH1 destructor declaration is:

   ~TH1() override;

The override is explicitly indicating that it replaces the virtual function from the base class. (TNamed has a similar declaration and its base class TObject has the declaration with the virtual keyword).

So the TH1 and TH2 destructors are indeed virtual and there is no memory leak when using:

TH1* h = new TH1D(); then delete h;

Thank you, now I understood the situation! If there is no override key word will it still be virtual destructor?

Yes, it will still be virtual

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.