Analysis functions such as C ++ learning, learning functions
Analysis functions such as C ++ Learning
I. Recognition of destructor
Here we will briefly talk about the class destructor, which is a member function of the class. The name consists of the wave number and the class name. Looking at its name, we can probably think of its function. It is to execute the opposite operation of the constructor: release the resources used by the object and destroy non-static members.
Let's take a look at several features of the Destructor:
1. The function name is added before the class name ~, No parameter and no return value.
2. A class can have only one destructor. If no explicit definition is available, the system generates a default destructor ).
3. The Destructor cannot be overloaded. Every time a constructor is called, an destructor is called.
Talk with a program:
// By Mr_Listening, 06 08 2016
Class Date
{
Public:
Date (int year = 1990, int month = 1, int day = 1)
: _ Month (year), _ year (month), _ day (day)
{}
~ Date ()
{
Cout <"~ Date () "<this <endl;
}
Private:
Int _ year = 1990;
Int _ month;
Int _ day;
};
Void test ()
{
Date d1;
}
Int main ()
{
Test ();
Return 0;
}
If the object d1 is constructed in the test () function, the d1 in the test () scope should be destroyed. At this time, the Destructor will be called. The following is the output of the program. Of course, the constructor is called before building an object, which is not described here.
We know that in the constructor, the initialization of members is completed before the execution of the function body, and initialized according to the sequence in which the Members appear in the class. In the destructor, first, execute the function body, and then destroy the members in the reverse order of initialization.
Ii. Destroy and clean up?
We have been talking about the role of destructor is to release the resources used by the object and destroy the members after your class object leaves the scope. So what is destruction? Then proceed to the following:
Void test ()
{
Int a = 10;
Int B = 20;
}
Recall that we defined a variable in a function, and defined two variables a and B in the test function. After this function is generated, a and B will be destroyed (stack operations ). If it is a pointer to a dynamic space, we all know that we need to do it free, otherwise it will cause memory leakage.
Speaking of this, the situation in the class is the same. This is the reason for the empty body of the synthesis destructor, and the function does not need to be done, when the class object is out of scope, the system will release those members of your built-in type. But as mentioned above, if one of my members has a pointer variable pointing to a piece of memory that you can dynamically open up, you need to release it yourself as before, in this case, you need to write your release code inside the Destructor so that all your resources can be released when you call the destructor. (This is actually a useful part of the destructor, right)
Another point is that when a member of a class type object has a class type object, the destructor of this object will also be called in the destructor.
3. Does the Destructor prevent this type of object from being destroyed?
If we do not want to release the object by using the destructor, it is obviously not feasible to define it explicitly, because the compiler will generate the default synthesis destructor. Previously, we learned that if you want the system to generate your own constructor by default, you can use default. In fact, there is another thing called delete.
ClassDate {public: Date (intyear = 1990, intmonth = 1, intday = 1): _ year (year), _ month (month), _ day (day ){}~ Date () = delete; private: int_year = 1990; int_month; int_day ;};
If I write this and create a Date-type object under it, this object cannot be destroyed. In fact, the compiler does not allow this, and an error will be reported directly to us.
However, it actually allows us to dynamically create this class type object, such as: Date * p = new Date; although this is feasible, When you delete p, there will still be errors, let alone the reason.
.
So in this case, you cannot define an object or release the dynamically allocated object, so it is better not to use this method.
4. Note
Generally, when you explicitly define the destructor, you should also explicitly define the copy constructor and value assignment operations. Why ??
See the following changes:
ClassDate {public: Date (intyear = 1990, intmonth = 1, intday = 1): _ year (year), _ month (month), _ day (day) {p = newint ;}~ Date () {deletep;} private: int_year = 1990; int_month; int_day; int * p ;};
The Member has a dynamically opened pointer member and is deleted in the destructor. If you do not explicitly define the copy constructor, when you create d2 in this way: Date d2 (d1, we all know that the default copy constructor is a shortest copy, so the result will be that the members of d2 p and d1 p point to the same space, when I call the destructor, a space is released twice, and the program will crash!
Finally,
We know that the Destructor can be called, but can the constructor? How is the environment used?