Different methods of using C ++ destructor

Source: Internet
Author: User

The C ++ programming language can be seen as an upgraded version of the C language. Many of its application methods are similar to other programming languages. However, in some specific usage methods, there are still some different application methods. Here we will first look at some special application methods of C ++ destructor.

The Terminator (that is, the destructor) in C # is similar to the C ++ destructor. However, since the execution time of the terminator cannot be determined during compilation, the two are actually quite different. The time when the Garbage Collector calls the C # Terminator is a certain time before the last time the object is used, but before the application is closed. On the contrary, as long as an object (rather than a pointer) is out of the range (the range here refers to the scope), the C ++ destructor will be automatically called. I am a little skeptical about this, so I wrote the C ++ and C # Code respectively to check whether the situation is true.

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace ConsoleApplication1  
  6. {  
  7. class Program  
  8. {  
  9. static void Main(string[] args)  
  10. {  
  11. test();  
  12. }  
  13. static void test()  
  14. {  
  15. myPeople p = new myPeople();  
  16. Console.WriteLine("Complate");  
  17. }  
  18. }  
  19. class myPeople  
  20. {  
  21. public myPeople()  
  22. {  
  23. Console.WriteLine("Construct");  
  24. }  
  25. ~myPeople()  
  26. {  
  27. Console.WriteLine("Dispose");  
  28. }  
  29. }  

So I inserted a breakpoint in each method and F5 began to debug gradually. I found that the call without myPeople ended the executor call of the Main () method after the test () method was executed. Let's look at C ++.

 
 
  1. #include<iostream> 
  2. #include<string> 
  3. using namespace std;  
  4. class myPeople  
  5. {  
  6. public :  
  7. myPeople()  
  8. {  
  9. cout<<"Construct"<<std::endl;  
  10. }  
  11. ~myPeople()  
  12. {  
  13. cout<<"Dispose"<<std::endl;  
  14. }  
  15. };  
  16. void myMethod()  
  17. {  
  18. myPeople my;;  
  19. cout<<"Complate"<<std::endl;  
  20. }  
  21. int main()  
  22. {  
  23. myMethod();  

Through the above execution process, we will find that C # Calls the Terminator is quite different from C ++ destructor, just as the author mentioned above. C # It is not certain to clean up the resources of a class. The release of C ++ class resources starts to call the Destructor after the class has exceeded the scope.

Related Article

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.