zoukankan      html  css  js  c++  java
  • OOP BasicConstructor&Destructor

    Q:   
         In MFC Libraby, there is no doubt for the importance of  the class CObject. and in the defition of CObject, wen found an interesting thing, that is :the destructor of CObject is virtual. why the coder of MFC think that virtual destructors are necessary?(MOTO 2004)

    Explanation:
    e.g. we can create a class like this:
    class CBase
    {
    public:
        ~CBase(){.....};
    };

    class CChild:CBase
    {
    public:
        ~CChild(){.....};
    };
        
    main()
    {
    Child C;
    ...
    return 0;
    }

        when the program is running, because that a Child object --C is created, therefore before the constructor of Child is called, the constructor of Base should be called firstly. So, when C  is being killed, it should firstly call the desctructor of Child and then Base (comments: opposite sequence calling constructor and desctructor). that is to say. whatever if destructor is virtual function or not,  when the derived class object is killed, it must call the desctructor of base class orderly.
        Therefore, why does CObject make destructor virtual?
        That 's because Polymorphism.
        take examples continued with the above sample. if there are codes in main function like this:
        CBase *pBase;
        Child c;
        pBase =&c;
        delete pBase;
        then when pBase point is being killed, which destructor is being called, is that of CBase or CChild?
    the answer is CBase apparently (static compiling). But if the constructor of CBase is changed into virtual, when pBase is being killed, it will call the destructor of CChild  firstly and then CBase secondly.
           From the above sample, it seems that no errors at all. But if the constructor of Child allocate memory in Heap Area. while its destructor is not virtual. when pBase point is killed, the heap memory that is allocated to CChild Object will not be deallocate . then memory leak occurs!

    another Demo for shows:

    // virtual.cpp : Defines the entry point for the console application.
    //

    #include 
    "stdafx.h"
    #include 
    "string.h"
    #include 
    "stdlib.h"
    #include 
    "assert.h"
    class A
    {
    public:
        A(
    char *username)
        
    {
            
    int len;
            len
    =strlen(username);
            m_username
    =new char[len+1];//(char*)malloc(sizeof(len+1));
            strcpy(m_username,username);
            printf(
    "Username is %s\n",m_username);
        }

    /*
        virtual ~A()
        {
            delete m_username;
            printf("A is destructed\n");
        }

    */

         
    ~A()
        
    {
            delete m_username;
            printf(
    "A is destructed\n");
        }



    protected:
        
    char *m_username;

    }
    ;

    class B:public A
    {
    public:
        B(
    char *username,char *password):A(username)
        
    {
            
    int len=strlen(password)+1;
            m_password
    =new char[len];//(char *)malloc(sizeof(len));
            strcpy(m_password,password);
            printf(
    "username:%s,password:%s\n",m_username,m_password);
        }

        
    ~B()
        
    {
            delete m_password;
            printf(
    "B is destructed\n");
        }

    protected:
        
    char *m_password;
    }
    ;

    int main(int argc, char* argv[])
    {
        B b(
    "herengang","982135");
        A 
    *a=&b;
        delete a;    
        
    return 0;


    }


    在这种情况下,我们看到的运行结果如下:

    可以看到,B的析构函数根本没有运行,这样在B中申请的heap空间 m_password就不会释放掉,从而造成memory leak!
            而如果我们将A的析构函数改称virtual之后,其运行结果如下:

            可以看到,所有heap上资源全部释放

  • 相关阅读:
    信息收集之DNS域名解析
    集合遍历利器 -- 迭代器模式 介绍 使用场景案例 优缺点及程序演示
    简单工厂的霸霸 -- 抽象工厂模式 介绍 使用场景案例 优缺点及程序演示
    可能是目前最易理解的手写promise
    chrome 开发者工具实用技巧
    gulp常用插件之modernizr使用
    koa-cors 跨域设置中间件
    在nodejs 中如何fs文件系统模块
    nodejs操作MongoDB数据库 MongoDB增、删、改、查
    vue动态渲染样式 class
  • 原文地址:https://www.cnblogs.com/Winston/p/1081807.html
Copyright © 2011-2022 走看看