Destructor and Variable Life Cycle

Source: Internet
Author: User

Destructor and Variable Life Cycle

This article introduces the destructor. It is my reading notes. I hope it is short but comprehensive enough to help you review. If some C ++ knowledge is unclear, it can help you quickly recall it.

The destructor name is the same as the class name, but "~" must be added before. If the Destructor does not have parameters or return values, it cannot be overloaded.

When to call the destructor

Destructor are automatically called when objects die. This is a situation that we all know, but it also implies: The parameter objects of the function and the objects returned as function values, the Destructor call is also triggered when the final death occurs.

1 # include <iostream> 2 using namespace std; 3 class CNum {4 public: 5 int num; 6 ~ CNum () {cout <"destructor" <endl ;}7 CNum () {num = 0; cout <"constractor CNum ()" <endl ;} 8 CNum (CNum & n) {num = n. num; cout <"constractor CNum (CNum & n)" <endl ;}9}; 10 11 CNum fun (CNum n) {12 cout <"fun () start "<endl; 13 n. num = 3; // The modification here is only the modification of the form parameter. It does not affect 14 return n; 15} 16 17 int main () {18 CNum n1; 19 n1 = fun (n1); 20 return 0; 21}

Running result:

Constractor CNum ()
Constractor CNum (CNum & n) // construct the parameter n
Fun ()
Constractor CNum (CNum & n) // construct a temporary object that is returned
The return value of destructor // fun is a temporary object. The life cycle of the temporary object is only one statement.
Destructor // leaves fun () and the parameter n disappears
Destructor // The n1 local variable disappears at the end of main
Press any key to continue...

When calling a function, you can use a reference instead of directly passing objects. This saves many unnecessary function calls, especially when the function is frequently called.

Virtual destructor

The Destructor can be a virtual function, but the constructor cannot. (Think about whether the constructor can be reloaded, but the Destructor cannot. This is even the case for the family)

Generally, if a class defines a virtual function, it is better to define the Destructor as a virtual function. The reason for this is that there are hidden bugs. Read a piece of code:

1 class CShape {2 public :~ CShape () {/* do nothing */} 3}; 4 5 class CCircle: public CShape {6 public: 7 CCircle () {/* Memory allocated here */} 8 ~ CCircle () {/* Memory released here */} 9}; 10 11 int main () {12 CShape * ptrShape = new CCircle (); 13 delete ptrShape; 14 return 0; 15}

So, which destructor does delete ptrShape execute? It's not exactly what we want to call ~ CCircle (), instead of calling nothing ~ CShape (), memory leakage. To avoid this, you can define the Destructor as a virtual function. That is, virtual ~ CShape () {/* do nothing */};. As long as the destructor of the base class is a virtual function, the destructor of the derived class will become a virtual destructor no matter whether it is declared with the keyword "role le.

Lifetime of a Variable

1) the life cycle of all variables starts from the beginning to the end of the program, and is defined first, and then extinct. The constructor of the global variable has been called before it enters main (), and it will not die after main () is introduced.

2) the life cycle of the temporary object is no longer than the statement execution time. Type conversion statements and return statements may generate temporary variables.

3) the lifetime of a local variable is from the definition to the last "}".

4) Static local variables are constructed and generated when the Definition Statement is executed for the first time. The global variables die before the end of the program.

 

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.