The destructor of C + + learning Research

Source: Internet
Author: User

The destructor of the destructor function.

In C + +, ' ~ ' is a bitwise inverse operator, and you can think of a destructor as a function contrary to a constructor function. Destructors have no return value, no function type, no parameters. Because there is no function argument, it cannot be overloaded, and a class can have more than one constructor, but there can be only a single destructor. Destructors do not delete objects, but rather do some cleanup work before undoing the memory occupied by the object, so that this portion of memory can be used by the program for new objects.

The destructor is automatically executed when the like lifetime Ends:

1> defines an automatic local object in a function, and when the function call ends, the object should be freed and the destructor automatically executed before the object is released.

The 2> static object does not release the object at the end of the function call, so it does not call the destructor, only the destructor of the static local object is called when the main function ends or the exit function is called to end the program.

3> If a global object is defined, the destructor for the global object is called when the program flow leaves its scope (when the main function ends or calls the Exit Function).

4> If an object is created dynamically with the new operator, the destructor of the object is called first when the object is disposed with the delete operator.

second, the order of calling constructors and destructors

In general, the first constructed post-destructor. There are other situations, however, because two objects may have different storage classes and different life cycles. Summarized as:

1> a global scope-defined object whose constructors are called before all functions in the file, including the main function, are executed. However, if there are multiple files in a program, and global objects are defined in different files, the order in which the constructors for those objects are executed is undefined. The destructor is called when the main function finishes executing or when the Exit function is called (this function terminates).

2> If an automatic local object is defined in a function, the constructor is called when the object is created. The destructor is called before the function call ends and the object is disposed. --If the function is called multiple times, the constructor is called every time the object is created.

3> if a static local object is defined in a function, the constructor is called once when the program is first called to establish the object, and the object is not disposed at the end of the call, so the destructor is not called, only when the main function or the Exit function ends the program.

The destructor of C + + learning Research

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.