What benefits do C ++ fictitious functions bring to us?

Source: Internet
Author: User

When we use the C ++ programming language for program development, we will gradually find that this language has brought great benefits to our program development. We know that C ++ fictitious functions used for base classes during C ++ development are generally virtual functions. But why? Here is a small example:

There are two classes:

 
 
  1. class ClxBase  
  2. {  
  3. public:  
  4. ClxBase() {};  
  5. virtual ~ClxBase() {};  
  6. virtual void DoSomething() { cout << "Do something 
    in class ClxBase!" << endl; };  
  7. };  
  8. class ClxDerived : public ClxBase  
  9. {  
  10. public:  
  11. ClxDerived() {};  
  12. ~ClxDerived() { cout << "Output from the destructor 
    of class ClxDerived!" << endl; };   
  13. void DoSomething() { cout << "Do something 
    in class ClxDerived!" << endl; };  
  14. }; 

Code

 
 
  1. ClxBase *pTest = new ClxDerived;  
  2. pTest->DoSomething();  
  3. delete pTest; 

The output result is:

 
 
  1. Do something in class ClxDerived!  
  2. Output from the destructor of class ClxDerived! 

This is very easy to understand.

However, if you remove the virtual before the ClxBase destructor, the output result is as follows:

 
 
  1. Do something in class ClxDerived! 

  • Summary of the Application Experience of C ++ traversal set
  • Detailed description of C ++ assignment function code
  • Analysis of C ++ variable declaration concepts
  • Detailed description of how C ++ implements WPF Animation
  • Detailed description of C ++ Chinese character-related application methods
That is to say, the destructor of the class ClxDerived is not called at all! Under normal circumstances, all the class destructor release the memory resources, and if the Destructor is not called, memory leakage will occur. I think all C ++ programmers know this danger. Of course, if you do other work in C ++ fictitious functions, all your efforts will be wasted.

Therefore, the answer to the question at the beginning of the article is -- This is to call the destructor of a derived class when a base class pointer is used to delete an object of A derived class.

Of course, it is not necessary to write all class destructor as virtual functions. When there is a virtual function in the class, the compiler will add a virtual function table to the class to store the virtual function pointer, which will increase the storage space of the class. Therefore, C ++ fictitious functions are written as virtual functions only when a class is used as the base class.

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.