c++ virtual deconstructor

#include iostream.h
class Base
{
   	public:
      	Base(){ cout<<"Constructing Base";}
      	
     // this is a destructor:
	
	~Base(){ cout<<"Destroying Base";}
};

class Derive: public Base
{
        public:
       	Derive(){ cout<<"Constructing Derive";}
       	
       	~Derive(){ cout<<"Destroying Derive";}
 };

void main()
{
    	Base *basePtr = new Derive();
        
        delete basePtr;
}

The output after running the code above would be:

Constructing Base  
Constructing Derive 
Destroying Base

Based on the output above, we can see that the constructors get called in the appropriate order when we create the Derive class object pointer in the main function.

But there is a major problem with the code above: the destructor for the "Derive" class does not get called at all when we delete ‘basePtr’.

So, how can we fix this problem?

Well, what we can do is make the base class destructor virtual, and that will ensure that the destructor for any class that derives from Base (in our case, its the "Derive" class) will be called.

Subscribe to our newsletter for more free interview questions.

Example with a Virtual Destructor:

So, the only thing we will need to change is the destructor in the Base class and here’s what it will look like – note that we highlighted the part of the code where the virtual keyword has been added in red:

class Base
{
   	public:
      	Base(){ cout<<"Constructing Base";}

	// this is a virtual destructor:
	virtual ~Base(){ cout<<"Destroying Base";}
};

Now, with that change, the output after running the code above will be:

Constructing Base  
Constructing Derive 
Destroying Derive
Destroying Base

Note that the derived class destructor will be called before the base class.

So, now you’ve seen why we need virtual destructors and also how they work.

One important design paradigm of class design is that if a class has one or more virtual functions, then that class should also have a virtual destructor.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值