fbpx

Complete C++ interview questions on destructors and virtual destructors

(Last Updated On: May 12, 2010)

Destructor:

If a base or derieved class does not have any members that are allocated on heap, would you still use a virtual destructor ? Why ?

If base class or derived class object is not created dynamically then there is no need for virtual destructor……
still need virtual destructor
Please tell us why we need virtual destructor if there is no dynamic allocation?
It is a good programming practice to keep the destructor virtual if your base class has any virtual function that is overridden in derived class.

Yes I do believe that there is need to keep the destructor virtual in the above when you have not allocated anything on the heap. Bcos objects on heap would clean up on its when when the derived class goes out of scope

I completely agree with this statement :” It is a good programming practice to keep the destructor virtual if your base class has any virtual function that is overridden in derived class.”

But as far as I know virtual function are implemented using vTable which is late binding. and should be implemented on heap. So there is no question of having virtual function.

Also the last sentence is bit confusing. Could you please clarify it.

run following code:

class Base{
public:
~Base(){cout<<"Base::~Base()"< NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Don't miss out!
Get my TRADE LIKE A BOSS 2 PDF Books

You will received instantly the download links.

Invalid email address
Give it a try. You can unsubscribe at any time.

NOTE!

Check NEW site on stock forex and ETF analysis and automation

Scroll to Top