C + + Virtual destructor

Source: Internet
Author: User

What does the Virtual keyword do?

Learn the function of virtual destructor by using the blog of netizens:

Virtual destructors are designed to avoid memory leaks and are used when there are pointer member variables in the subclass.

It is also said that the virtual destructor makes it possible to invoke the destructor of a subclass when deleting a pointer to the base class of the child class object to release the heap memory in the subclass, and to prevent memory leaks.

A function call in C + + does not apply dynamic binding by default, and to trigger dynamic binding, two conditions must be met: The first is a virtual function; The second is called by a reference or pointer to a base class type

Virtual inheritance and examples and considerations

Virtual can be used to define class functions and apply to virtual inheritance

Attention:

A meta-function, a constructor, a static function cannot be decorated with the virtual keyword. Ordinary member functions and destructors can be decorated with the virtual keyword.

  

#include <stdlib.h>#include<iostream>using namespacestd;classGrandfather { Public: Grandfather () {}Virtual voidFun () {cout<<"Grandfather Call Function"<<Endl; }};classFather: PublicGrandfather { Public: Father () {}voidFun () {cout<<"Father Call Function"<<Endl; }};classSon: PublicFather { Public: Son () {}voidFun () {cout<<"Son Call Function"<<Endl; }};voidPrint (Grandfather *father) {Father-Fun ();}intMainintargcChar Const*argv[]) {Father*pfather =NewSon; Pfather-Fun (); DeletePfather;//grandfather *grandfather = new Father;//Print (grandfather);    return 0;}
class-Custom virtual functions with custom virtual

return value:

Son Call function

Let's take a look at the first example:

  

#include <iostream>using namespacestd;classGraphfather {Private: Public: Graphfather () {cout<<"Graphfather Structure"<<'\ n';    }; ~Graphfather () {cout<<"Graphfather Deconstruction"<<'\ n'; };};classFather: PublicGraphfather {Private: Public: Father () {cout<<"Father Structure"<<"\ n";    }; ~Father () {cout<<"Father Deconstruction"<<"\ n"; };};classSon: PublicFather {Private: Public: Son () {cout<<"Son Structure"<<"\ n";    }; ~Son () {cout<<"Son Deconstruction"<<"\ n"; };};intMainintargcChar Const*argv[]) {Graphfather*gfather =NewSon; DeleteGfather; cout<<"================"<<'\ n'; Father*father =NewSon; Deletefather; cout<<"================"<<'\ n'; Son*son =NewSon; DeleteSon; cout<<"================"<<'\ n'; return 0;};

return value:

  

graphfather structurefather structureson structuregraphfather Deconstruction  -----"Look here =============== =Graphfather structurefather structureson structurefather deconstruction  ------See here Graphfather Deconstruction================graphfather structurefather structureson structureson Deconstruction  -- -----Look here Father Deconstructiongraphfather Deconstruction================

This just understand the order of execution, but the Netizen's blog is very good, record just for better understanding and review

Better understanding of the role of virtual, and the use of scenarios

#include <iostream>using namespacestd;classbase{ Public: Base () {}~Base () {cout<<"Base destructor."<<Endl; }Private:    intA, B;};classDerive: Publicbase{ Public: Derive () {}~Derive () {cout<<"Derive destructor."<<Endl; //Release Memeory        if(PI! =NULL) {            DeletePI; }    }Private:    int*PI;};intMainvoid) {{Base*PD =NewDerive; DeletePD; }    inti; CIN>>i; return 0;}
View Code

The result is that only the destructor of the base class is executed, and there are no free pointers in the derived class, which creates a memory leak

This is also a function of virtual destructor

Add virtual destructor to base class base virtual ~base () ...

#include <iostream>using namespacestd;classBase { Public: Base () {}Virtual~Base () {cout<<"Base destructor."<<Endl; }Private:    intA, B;};classDerive: PublicBase { Public: Derive () {PI=New int; }    ~Derive () {cout<<"Derive destructor."<<Endl; //Release Memeory        if(PI! =NULL) {            DeletePI; }    }Private:    int*PI;};intMainintargcChar Const*argv[]) {{Base*PD =NewDerive; DeletePD; }    inti; CIN>>i; return 0;}

Reference blog1:https://www.cnblogs.com/lit10050528/p/3874703.html

Reference blog2:http://www.cnblogs.com/cxq0017/p/6550125.html-"This blog code does not have a Delete object pointer, you need to note

C + + Virtual destructor

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.