Oop basic-constructor & destructor

Source: Internet
Author: User

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 shoshould be called firstly. so, when C is being killed, it shoshould 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 ("Herengang","982135");
A*A= &B;
Delete;
Return 0;


}

in this case, the running result is as follows:
the destructor of B is not running, in this way, the requested heap space m_password in B will not be released, resulting in memory leak!
if we change the destructor of A to virtual, the running result is as follows:

all heap resources are released

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.