|
Reg. Base classes destructors without the keyword : virtual |
Posted by Jan K. Nielsen on Aug-01-2023 03:11 |
|
Hello
My intel compiler discovered that ViewPortManager, ViewPortControlBase are missing the keyword : virtual
Is this on purpose ?
I have read that the destructors of the of the children won't be called if the base class destructor is not virtual.
Best, regards.
Jan |
Re: Reg. Base classes destructors without the keyword : virtual |
Posted by Peter Kwan on Aug-01-2023 18:17 |
|
Hi Jan,
If the base class destructor is non-virtual, the destructors of the derived class will not be called if you allocated the derived class on the heap (eg. using "new DerivedClass()") but the code do not delete the derived class but instead delete the base class. For example:
DerivedClass *d = new DerivedClass();
BaseClass *b = d;
// Delete b will not call the destructor of d. If the code delete d, the destructor of
// both d and b will be called.
delete b;
For the ViewPortManager, it is used in GUI controls with multiple inheritance. For example, the MFC control CChartViewer inherits both from the MFC CStatic control and the ViewPortManager. We expect the programmer to treat CChartViewer as a GUI control and will handle it just like any other GUI controls, and will delete the control if necessary (as opposed to deleting the ViewPortManager). In many cases, the lifetime of the GUI controls are automatically handle by the programming framework. For example, the MFC control may be initiated in a dialog generated with a dialog resource editor, and the framework will automatically delete the GUI control when the dialog closes. So no issue should occur in real usage. The same applies to ViewPortControlBase.
Best Regards
Peter Kwan |
|