zoukankan      html  css  js  c++  java
  • 虚析构函数

    通过基类的指针在删除派生类对象时只调用了基类的析构函数,而没有调用派生类的析构函数,因此在有些程序中会存在问题。(例如需要在构造函数与析构函数中进行计数统计的程序)

    实际效果:

    删除一个派生类对象时

    1. 先调用派生类的析构函数
    2. 再调用基类的析构函数

    解决办法:

    把基类的析构函数声明为virtual

    • 派生类的析构函数virtual不用声明
    • 通过基类的指针删除派生类对象时
      • 先调用派生类的析构函数
      • 后调用基类的析构函数

    类如果定义了虚函数,则最好将析构函数也定义成虚函数。不允许构造函数是虚函数。

    代码如下:

    #include <iostream>
    
    using namespace std;
    
    class Son
    {
    public:
    virtual ~Son()
    {
    cout << "the destructor of son is called" << endl;
    }
    };
    class Grandson : public Son
    {
    public:
    ~Grandson()
    {
    cout << "the destructor of Grandson is called " << endl;
    }
    };
    
    int main()
    {
    Son *p = NULL;
    p = new Grandson;
    delete p;
    return 0;
    }

    程序运行结果为:

     

    当析构函数不是virtual时,运行结果为:

      

    纯虚函数和抽象类

    没有函数体的虚函数叫纯虚函数。

    包含纯虚函数的类叫抽象类。

    • 只能作为基类来派生新类使用。
    • 不能创建抽象类的对象。
    • 抽象类的指针和引用指向派生类生出来的类的对象。

    抽象类中:

    • 在成员函数中可以调用纯虚函数
    • 在构造函数和析构函数中不能调用纯虚函数
    • 如果一个类从抽象类派生而来,只有当它实现了基类中所有的纯虚函数,才能成为非抽象类。

    实验代码如下:

    #include <iostream>
    
    using namespace std;
    
    class cSon
    {
    public:
        virtual void f() = 0;
        void g(void)
        {
            f();
        }
    };
    class cgrandson :public cSon
    {
    public:
        void f()
        {
            cout << "f() is called" << endl;
        }
    };
    int main()
    {
        cgrandson b;
        cSon *p = &b;
        p->g();
        return 0;
    }

    运行结果:

     

     参考链接:

    https://www.coursera.org/learn/cpp-chengxu-sheji

  • 相关阅读:
    Go语言从入门到放弃(三) 布尔/数字/格式化输出
    11. GLOBAL_VARIABLES 与 SESSION_VARIABLES
    10. GLOBAL_STATUS 与 SESSION_STATUS
    9. FILES
    8. EVENTS
    7. ENGINES
    6. COLUMN_PRIVILEGES
    5. COLUMNS
    4. COLLATION_CHARACTER_SET_APPLICABILITY
    3. COLLATIONS
  • 原文地址:https://www.cnblogs.com/helloforworld/p/5655313.html
Copyright © 2011-2022 走看看