(Note): constructor and destructor, note Constructor

Source: Internet
Author: User

(Note): constructor and destructor, note Constructor

1. constructor and destructor

  C ++ member variables in the class cannot be directly used to reset the memory occupied by the variables. Otherwise, in addition to assigning values, the program may fail, therefore, in order not to let this happen, C ++ has designed a Constructor that has the same Constructor and Class Name and has no return type. The Destructor is to "Clean up the aftermath. The system automatically calls this function when the Scope of the instance of the class is exceeded. In short, constructor has the default constructor (with no parameters) and self-defined Constructor (with parameters. The copy constructor is not described at the moment. The following is a simple description of the usage of constructor and destructor through a piece of code and the annotation indicated in the Code. The program source code is as follows (click it )(The serial numbers marked as (1) (2) (3) in the code will be explained in detail in the comment area.):

1 # include <iostream> 2 using namespace std; 3 class X {4 5 public: 6 int x; 7 int * p; 8 X () {}// (1) 9 X (int y); // (2) 10 ~ X (); // (3) 11}; 12 X: X (int y) // note that there is no return type for void because neither the Destructor nor constructor returns type 13 {14 p = new int; // apply for resources (4) 15 x = y; // This is actually called a value assignment instead of initializing 16 cout. <"the value of the constructor is:" <x <endl; 17} 18 X ::~ X () 19 {20 delete p; // release resource 21 // (5) 22 cout <"the variable value is" <x <"destructor" <endl; // The Order of exiting the constructor is 23} 24 int main () 25 {26 X a (12 ); // at this time, the form parameter 27 28 {29 cout <"Enter scope of variable B" <endl; 30 X B (10) of the constructor is given to 12 ); 31 cout <"Exit Scope of variable B" <endl; 32} 33 34 return 0; 35}
View Code

Note area:

(1) This is the default constructor. If we do not display the defined constructor, the compiler will provide a default constructor called the merged constructor.
(2) The constructor is automatically called during instantiation. // note that this is no longer the default constructor.
(3) The Destructor is automatically called before the class exceeded the scope.
(4) You can construct a function to apply for a space and release it in the destructor.
(5) // you can release some applied memory resources.
// For example, delete p or delete [] p. Here, P is applied in the constructor.
Note:
// New int;
// New int *;
// New int * [10];
// Delete p;
// Delete [] p; // remember that this bracket is used only when new [] (representing the array form) (recorded in the following note image)

Test results:

 

Result description:

When defining a variable, the system automatically calls the constructor. When the scope of the variable is exceeded, the system automatically calls the destructor.

Note:

If you change the image below in the main function based on the above Code, no errors will occur during compilation, but errors will occur during running. The program crashes because no other destructor is called. However, inside the class X Declaration, there is no error in the form of memory in the new array form in the constructor and delete p in the destructor, because delete deletes the memory that p points to and does not involve destructor, no error will occur. For more information, see the following summary.

To sum up, take notes on delete p and delete [] p, As shown in:

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.