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

  • 相关阅读:
    并发编程之多线程理论
    僵尸进程和孤儿进程
    并发编程之多进程
    并发编程之多进程理论
    操作系统介绍
    面向对象和网络编程练习题
    网络编程——socket编程
    面向对象练习题
    面向对象软件开发实战
    异常处理
  • 原文地址:https://www.cnblogs.com/Winston/p/1081807.html
Copyright © 2020-2023  润新知