The origin of constructor destructor

Source: Internet
Author: User

All the Syntax problems have indeed helped programmers a lot. However, after the program passes the compilation check, it does not mean that the error does not exist. In the "error" family, the "syntax error" status can only be regarded as a younger brother. High-level errors are often hidden in depth, just like a sly criminal. It is not easy to catch him.

Based on experience, many program errors that are hard to detect are caused by the variable being correctly initialized or cleared, and initialization and cleanup are easily forgotten. Stroustrup fully considers this problem when designing the C ++ language and solves it well: Put the initialization work of the object in the constructor, and put the cleanup work in the destructor. When an object is created, the constructor is automatically executed. When an object dies, the Destructor is automatically executed. You don't have to worry about object initialization and cleanup.

The name of the constructor and the Destructor cannot start at will, and must be recognized by the compiler before it can be automatically executed. The naming method of stroustrup is simple and reasonable: the constructor and destructor have the same name as the class. because the purpose of the constructor is opposite to that of the constructor, the prefix '~ 'To show the difference.

Except for the name, the constructor and the Destructor have no return value type, which is voidDifferent functions. The mission of constructor and destructor is very clear, just like birth and death. If they have return value types, the compiler will be overwhelmed. In order to prevent out-of-the-box branches, it is simply specified that there is no return value type.

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.