C ++ destructor and this pointer

Source: Internet
Author: User

In the C ++ destructor, the primary component is the address table of a class virtual function. This table solves the inheritance and overload problems and ensures that it can reflect the actual function in a real sense, now let's explore the mysteries of the C ++ destructor...

The Destructor is not described in the previous examples, because the classes used do not need to be cleaned up at the end. The following program provides a new Date class, which includes a string pointer to indicate the month.

 
 
  1. #include \"iostream.h\"  
  2.        #include \"string.h\"  
  3.  
  4.        class Date  
  5.        {  
  6.            int mo,da,yr;  
  7.            char *month;  
  8.        public:  
  9.            Date(int m=0, int d=0, int y=0);  
  10.            ~Date();  
  11.            void display() const;  
  12.        }; 

In the Date object constructor, the memory is dynamically allocated to the month string using the new operator, and the month name is copied from the internal array to the month string pointer. The Destructor may have some problems when deleting the month pointer. Of course, from the perspective of this program itself, there is no trouble; but from the perspective of designing a class, when the Date class is used for value assignment, there will be problems. Assume that the above main () is changed to"

 
 
  1. #include \"iostream.h\"  
  2.        #include \"string.h\"  
  3.  
  4.        class Date  
  5.        {  
  6.            int mo,da,yr;  
  7.            char *month;  
  8.        public:  
  9.            Date(int m=0, int d=0, int y=0);  
  10.            ~Date();  
  11.            void display() const;  
  12.        }; 

This generates an empty Date variable named today and assigns it the birthday value. If the compiler is not specifically notified, it simply assumes that the assignment of a class is a member copy of the member. In the above program, the C ++ destructor variable birthday has a struct pointer month, and the new operator has been initialized in the constructor. When birthday leaves its scope.

The Destructor calls the delete operator to release the memory. At the same time, when today leaves its scope, the Destructor will release it, and the month pointer in today is a copy of the month pointer in birthday. The Destructor deletes the same pointer twice, which has unpredictable consequences.

This is also true in C ++ destructor. If you have taken this attitude from the very beginning of C ++ learning, even if you are asked about the details of a language during the interview, you can also say with confidence that you do not know the details, but when you encounter problems in the actual encoding, you will find appropriate references and quickly solve the problem, is the ultimate goal ).

  • Function Description in C ++
  • Illustration C ++ Multithreading
  • Notes for using C ++ Builder
  • Analysis of Visual C ++ development and implementation methods
  • Exploring questions about C ++

Of course, the bigger possibility is that you have already been familiar with the most common 80% traps and skills in normal coding, because you are using practical guidance for learning, so the traps and skills you need to learn are almost certainly common scenarios, it is N times more efficient to catch a C ++ "classic" than to study it in detail, because without practical experience, you may think that each technique and trap have the same probability of attack.

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.