---- Structure and Analysis (Turning)

Source: Internet
Author: User

Caused
---- Structure and Structure
Author: HolyFire
In our daily life, we usually sum up some rules. When we get up in the morning, we brush our teeth, wash our faces, and take a bath and sleep at night. Open the bottle cap before using the bottled condiments. This is a good habit. But some people are different, and they are often lazy. A person who often does not brush his teeth, wash his face, or take a bath may experience things. The room is not neat. These are what we don't want to see. Of course, we do not want to mess up the code in programming.
Using an uninitialized variable is a disaster. Using an uninitialized pointer will cause a crash. This is my advice. In C ++, initialization will not have any additional effect or reduce efficiency. What we need to do is to develop a good habit and initialize an object when it is generated.
For
Object. Init ();
Object. Free ();
This call is not very difficult. It is not difficult to remember him, but no one can guarantee that he will never forget it. What's worse
Object. Init ();
Object. Free ();
Not paired
Object. Init ();
Object. Free ();
Object. Free ();
Or
Object. Init ();
Object. Init ();
Object. Free ();
No one knows what the results will bring, and the compiler will not report such errors. This is a terrible mistake. What a programmer is most afraid of is a logical mistake. It may take a whole day to find such a mistake.
Let's see if there is any good solution.
An object is analyzed by time. Generally, there are three stages: birth, activity, and death. What do we need to do? initialize, run, and release. Well, let's take a look at it. We found that the object was initialized when it was born and released when it died. If all of this can be done using this mechanism, we no longer have to worry about the trouble caused by forgetting or making mistakes.
C ++ provides such a mechanism. He has an agreement to use it.
Class Object {
Public:
Object (); // a function with the same name as a class. This function has no return value, which is called a constructor.
~ Object (); // Similarly, add an anti-sign before the constructor name, called the destructor.
};
The constructor is called when an object is generated.
The Destructor will be called when the object is destroyed.
The calling process and implementation method are completed by the compiler. We only need to remember the call time, and their calls are automatically completed without our control.
# Include <iostream>

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.