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上资源全部释放

  • 相关阅读:
    Linux 操作memcache命令行
    查看memcache版本
    磊哥测评之数据库SaaS篇:腾讯云控制台、DMC和小程序
    一看就能学会的H5视频推流方案
    JavaScript与WebAssembly进行比较
    Android调试神器stetho使用详解和改造
    5分钟入门git模式开发
    深耕品质,腾讯WeTest《2018中国移动游戏质量白皮书》正式发布
    RSA签名的PSS模式
    附实例!图解React的生命周期及执行顺序
  • 原文地址:https://www.cnblogs.com/Winston/p/1081807.html
Copyright © 2011-2022 走看看