C++中析构函数为何是虚函数

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++中析构函数为何是虚函数

The Laughing Uncle   2022-11-26 我要评论

析构函数为什么是虚函数

虚构函数是虚函数的情况只需要在特定场景下出现即可,正常情况下不必要弄成虚函数。

如果基类的析构函数不是虚函数,在特定情况下会导致派生来无法被析构。

  • 情况1:用派生类类型指针绑定派生类实例,析构的时候,不管基类析构函数是不是虚函数,都会正常析构
  • 情况2:用基类类型指针绑定派生类实例,析构的时候,如果基类析构函数不是虚函数,则只会析构基类,不会析构派生类对象,从而造成内存泄漏。为什么???因为析构的时候如果没有虚函数的动态绑定功能,就只根据指针的类型来进行的,而不是根据指针绑定的对象来进行,所以只是调用了基类的析构函数;如果基类的析构函数是虚函数,则析构的时候就要根据指针绑定的对象来调用对应的析构函数了。

构造函数为什么不能出虚函数

因为类的虚函数表指针是在构造函数中初始化的,这时候如果构造函数本身是虚函数,又应该由谁来初始化它的虚函数指针呢,所以构造函数不能是虚函数。

为什么构造函数和析构函数都不能调用虚函数

    #include <iostream.h>

    using namespace std;

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

    // 派生类
    class Derive: public Base{
        public:
            Derive(){
                cout << "Derive::Derive()\n";
                fun();
            }
            ~Derive(){
                cout << "Derive::Derive()\n";
                fun();
            }
            virtual void fun(){
                cout << "Derive::fun() virtual\n";
            }
    };

    int main()
    {
        Base *bd = new Derive();  // 基类Base的指针bd指向的是派生类Derive的对象
        delete bd;
        return 0;
    }

对于上述情况,基类指针指向派生类对象。构造时,先调用基类Base的构造函数,此时构函数中调用基类中的fun()函数,此时虚函数的动态绑定机制并没有会生效,这是因为此时的派生类还不存在。析构时,先析构派生类,派生类中的fun()函数调用的是自己的fun(),然后析构基类Base,基类析构函数中的fun()调用的是基类Base自己的fun()函数,这里虚函数的动态绑定机制也没有生效,因为此时派生类已经不存在了。

不要在构造函数中调用虚函数的原因:因为父类对象会在子类之前进行构造,此时子类部分的数据成员还未初始化, 因此调用子类的虚函数是不安全的,故而C++不会进行动态联编。

不要在析构函数中调用虚函数的原因:析构函数是用来销毁一个对象的,在销毁一个对象时,先调用子类的析构函数,然后再调用基类的析构函数。所以在调用基类的析构函数时,派生类对象的数据成员已经“销毁”,这个时再调用子类的虚函数已经没有意义了。

c++基类的析构函数为虚函数的原因

原因

在实现多态时, 当用基类指针操作派生类, 在析构时候防止只析构基类而不析构派生类。

例子

(1): 

#include<iostream>
  using namespace std;
  class Base{
  public:
     Base() {};
    ~Base() {cout << "Output from the destructor of class Base!" << endl;};
 
    void DoSomething() { cout << "Do something in class Base!" << endl; };
  };
 
  class Derived : public Base{
  public:
     Derived() {};
    ~Derived() { cout << "Output from the destructor of class Derived!" << endl; };
 
    void DoSomething() { cout << "Do something in class Derived!" << endl; };
  };
  int  main()
   { 
     Derived* p = new Derived;
     p->DoSomething();
     delete p;
     return 0;
  }

运行结果:

Do something in class Derived!           

Output from the destructor of class Derived!

Output from the destructor of class Base! 

代码中基类的析构函数不是虚函数,在main函数中用继承类的指针去操作继承类的成员,释放指针P的过程是:先释放继承类的资源,再释放基类资源。

(2):

  #include<iostream>
  using namespace std;
  class Base{
  public:
     Base() {};
    ~Base() {cout << "Output from the destructor of class Base!" << endl;};
 
    void DoSomething() { cout << "Do something in class Base!" << endl; };
  };
  class Derived : public Base{
  public:
     Derived() {};
    ~Derived() { cout << "Output from the destructor of class Derived!" << endl; };
 
    void DoSomething() { cout << "Do something in class Derived!" << endl; };
  };
  int  main(){ 
     Base* p = new Derived;
     p->DoSomething();
     delete p;
     return 0;
   }

运行结果:

Do something in class ClxBase!
Output from the destructor of class ClxBase!

代码中基类的析构函数同样不是虚函数,不同的是在main函数中用基类的指针去操作继承类的成员,释放指针P的过程是:只释放基类的资源,而没有调用继承类的析构函数。 调用DoSomething()函数执行的也是基类定义的函数。

一般情况下,这样的删除只能够删除基类对象,而不能删除子类对象,形成了删除一半形象,造成内存泄漏。

在公有继承中,基类对派生类及其对象的操作,只能影响到那些从基类继承下来的成员。如果想要用基类对非继承成员进行操作,则要把基类的这个函数定义为虚函数。 析构函数自然也应该如此:如果它想析构子类中的重新定义或新的成员及对象,当然也应该声明为虚的。

(3): 

#include<iostream>
  using namespace std;
  class Base{
  public:
     Base() {};
    virtual ~Base() {cout << "Output from the destructor of class Base!" << endl;};
 
    virtual void DoSomething() { cout << "Do something in class Base!" << endl; };
  };
  class Derived : public Base{
  public:
     Derived() {};
    ~Derived() { cout << "Output from the destructor of class Derived!" << endl; };
 
    void DoSomething() { cout << "Do something in class Derived!" << endl; };
  };
  int  main(){ 
     Base* p = new Derived;
     p->DoSomething();
     delete p;
     return 0;
   }

运行结果:

Do something in class ClxDerived!
Output from the destructor of class ClxDerived!
Output from the destructor of class ClxBase!

代码中基类的析构函数被定义为虚函数,在main函数中用基类的指针去操作继承类的成员,释放指针P的过程是:释放了继承类的资源,再调用基类的析构函数。调用DoSomething()函数执行的也是继承类定义的函数。

小结:基类指针可以指向派生类的对象(多态性),如果删除该指针delete p;就会调用该指针指向的派生类析构函数,而派生类的析构函数又自动调用基类的析构函数,这样整个派生类的对象完全被释放。

如果析构函数不被声明成虚函数,则编译器实施静态绑定,在删除基类指针时,只会调用基类的析构函数而不调用派生类析构函数,这样就会造成派生类对象析构不完全。所以,将析构函数声明为虚函数是十分必要的。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们