Destructors such as C + +

Source: Internet
Author: User
Tags shallow copy

First, the understanding of the destructor function

The constructor of the class is described in my previous blog post. http://www.cnblogs.com/MrListening/p/5557114.html.

here, let's briefly talk about the destructor of a class, which is a member function of a class whose name consists of a tilde plus a class name. Look at its name, we'll probably think of him. function, which is to perform the opposite of the constructor: disposes the resource used by the object and destroys the non-static member.

Again, let's look at some of the features of the destructor:

1. The function name is preceded by the class name with a ~, no parameter, and no return value.

2. A class can have only one destructor, and if there is no explicit definition, the system generates a default destructor (the composition destructor).

3. Destructors cannot be overloaded. Every call to a constructor will have a call to the destructor.

Take the program to speak:

//by mr_listening,06classdate{ Public: Date (intYear=1990,intMonth=1,intday=1): _month (year), _year (month), _day (day) {}~Date () {cout<<"~date ()"<< This<<Endl; }Private:    int_year=1990; int_month; int_day;};voidTest () {Date d1;}intMain () {test (); return 0;}

when the object D1 is constructed in the test () function, the D1 in the Out test () scope should be destroyed, the destructor is called, and the output of the program is shown below. Of course, the constructor is called first when the object is built, and is not explained here.

we know that in constructors, the initialization of a member is done before the body of the function is executed and initialized in the order in which the member appears in the class, whereas in the destructor, the function body is executed first, then the member is destroyed, and the member is destroyed in reverse initialization.

second, destroy, clean up?

We've been saying that the function of a destructor is to release the resources used by the object after your class object leaves the scope and destroy the members. So what exactly is the destruction of the word here? So keep looking down:

void Test ()

{

int a=10;

int b=20;

}

Recall that we defined a variable in a function body, defined A and B two variables in the test function, then after this function, A and B will be destroyed (the operation on the stack). So if it's a pointer to a space that's dynamically opened up, we all know that we need to do it ourselves, otherwise it will cause a memory leak.

Speaking of which, in fact, the situation in the class is the same as this, that is why the composition destructor body is empty, the function does not need to do anything, when the class object out of scope, the system will release those members of your built-in type. But as I said above, if I have a pointer variable in my member and point to a piece of memory that you are dynamically opening up, then you need to release it yourself as before, and you need to write your release code inside the destructor so that all of your resources can be freed when the destructor is called. (actually that's where the destructor works, right?)

Then again, when a member of a class-type object has a class-type object, the destructor for that object is also called in the destructor.

Third, destructors to prevent this type of object from being destroyed?

If we do not want destructors to dispose of objects, it is obviously not possible to explicitly define them, because the compiler generates a default composition destructor. We knew before. If you want the system to generate its own constructor by default, you can take advantage of default, then there is actually a thing called delete.

Class date{public:date (int year=1990,int month=1,int day=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 at the bottom, then this object will not be destroyed, but the compiler does not allow it, and will give us an error directly.

but it's actually allowing us to dynamically create objects of this class type, like this: date* p = new Date; Although this is possible, when you delete p, you still get the wrong reason.

So if you do this, you can neither define an object nor release dynamically allocated objects, so don't use it as a good thing.

Four, attention

In general, if you explicitly define a destructor, you should also explicitly define the copy constructor and assignment operation. Why??

See the following changes:

Class date{public:date (int year=1990,int month=1,int day=1): _year (year), _month (month),  _day (day) {p = new int;} ~date () {delete p;} Private:int _year=1990;   int _month;int _day;int *p;};

Members have dynamically opened pointer members, in the destructor of the delete, if you do not explicitly define the copy constructor, when you do: Date D2 (D1) To create D2, we all know that the default copy constructor is a shallow copy, So the result will be that the D2 members P and D1 p is pointing to the same block of space, then call the destructor back to cause a space to be released two times, the program will crash Oh!

At last

We know that destructors are callable, so can constructors? How the environment is used

Destructors such as C + +

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.