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!
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;
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: