c++ 虚拟析构函数_C ++中的虚拟析构函数

c++ 虚拟析构函数

Destructors in the Base class can be Virtual. Whenever Upcasting is done, Destructors of the Base class must be made virtual for proper destrucstion of the object when the program exits.

基类中的析构函数可以是虚拟的。 无论何时进行上播,都必须将基类的析构函数设为虚拟,以在程序退出时对对象进行适当的解构。

NOTE: Constructors are never Virtual, only Destructors can be Virtual.

注意:构造函数从不虚拟,只有析构函数可以是虚拟。

在C ++中不使用虚拟析构函数进行上载 (Upcasting without Virtual Destructor in C++)

Lets first see what happens when we do not have a virtual Base class destructor.

首先让我们看看当我们没有虚拟基类析构函数时会发生什么。

class Base
{
    public:
    ~Base() 
    {
        cout << "Base Destructor\n"; 
    }
};

class Derived:public Base
{
    public:
    ~Derived() 
    { 
        cout<< "Derived Destructor\n"; 
    }
}; 

int main()
{
    Base* b = new Derived;     // Upcasting
    delete b;
}

Base Destructor

基础析构函数

In the above example, delete b will only call the Base class destructor, which is undesirable because, then the object of Derived class remains undestructed, because its destructor is never called. Which results in memory leak.

在上面的示例中, delete b将仅调用基类的析构函数,这是不希望的,因为然后,派生类的对象仍保持未析构状态,因为从未调用过它的析构函数。 导致内存泄漏。

在C ++中使用虚拟析构函数进行上载 (Upcasting with Virtual Destructor in C++)

Now lets see. what happens when we have Virtual destructor in the base class.

现在让我们看看。 当基类中有虚拟析构函数时会发生什么。

class Base
{
    public:
    virtual ~Base() 
    {
        cout << "Base Destructor\n"; 
    }
};

class Derived:public Base
{
    public:
    ~Derived() 
    { 
        cout<< "Derived Destructor"; 
    }
}; 

int main()
{
    Base* b = new Derived;     // Upcasting
    delete b;
}

Derived Destructor Base Destructor

派生析构函数基析构函数

When we have Virtual destructor inside the base class, then first Derived class's destructor is called and then Base class's destructor is called, which is the desired behaviour.

当我们在基类中具有Virtual析构函数时,首先将调用Derived类的析构函数,然后再调用Base类的析构函数,这是所需的行为。

C ++中的纯虚拟析构函数 (Pure Virtual Destructors in C++)

  • Pure Virtual Destructors are legal in C++. Also, pure virtual Destructors must be defined, which is against the pure virtual behaviour.

    纯虚拟析构函数在C ++中是合法的。 另外,必须定义纯虚拟析构函数,这与纯虚拟行为相反。

  • The only difference between Virtual and Pure Virtual Destructor is, that pure virtual destructor will make its Base class Abstract, hence you cannot create object of that class.

    虚拟和纯虚拟析构函数之间的唯一区别是,纯虚拟析构函数将使它的基类成为Abstract,因此您不能创建该类的对象。

  • There is no requirement of implementing pure virtual destructors in the derived classes.

    不需要在派生类中实现纯虚拟析构函数。

class Base
{
    public:
    virtual ~Base() = 0;     // Pure Virtual Destructor
};

// Definition of Pure Virtual Destructor
Base::~Base() 
{ 
    cout << "Base Destructor\n"; 
} 

class Derived:public Base
{
    public:
    ~Derived() 
    { 
        cout<< "Derived Destructor"; 
    }
};

翻译自: https://www.studytonight.com/cpp/virtual-destructors.php

c++ 虚拟析构函数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值