Constructors and destructors in C + +

Source: Internet
Author: User

constructor Function: C + + provides a constructor (constructor) to handle initialization of an object. executed automatically when an object is created. The name of the constructor must be the same as the class name, it does not have any type, and does not return any values.

Summary of constructor functions:

The ① constructor is a special function used in C + + to initialize the state of an object.

The ② constructor is called automatically when the object is created (the default call), and is called Incognito.

③ constructors and ordinary member functions follow overloaded rules.

The ④ copy constructor is an important guarantee for proper initialization of objects, and it is necessary to manually write a copy constructor.

Call to constructor:

Automatic invocation : In general, the C + + compiler automatically calls constructors

Call Manually: In some cases, you need to call the constructor manually

There are three types of constructors:

There are parameter constructors, default constructors, copy constructors.

destructor : the destructor (destructor) is also a special member function, which functions in contrast to the constructor, whose name is preceded by a "~" symbol in the name of the class, and the destructor is a function opposite to the constructor function.

Summary of Destructors:

① if an object is defined in a function (it is an auto-local object), when the function is called at the end, the object should be freed and the destructor automatically executed before the object is released.

②static a local object is not disposed at the end of a function call and therefore does not call a destructor, only the destructor of the static local object is called when the main function ends or the exit function is called to end the program.

③ If a global object is defined, the destructor for the global object is called when the program's process leaves its scope, such as when the main function ends or calls the Exit function.

④ If an object is created dynamically with the new operator, the destructor for that object is called first when the object is disposed with the delete operator.

There are three calling methods for the parameter constructor:

Class test{public://has the parameter constructor Test (int a) {m_a = A;} Parameterless constructor Test () {m_a = 0;} Assignment constructor (copy constructor) Test (const Test &obj) {}public:void print () {cout<< "M_a" <<M_A<<ENDL;} Protected:private:int m_a;}; void Main () {Test T1 ();//c++ compiler automatically calls the class's parameter constructor T1.print (); Test t2 = 20; The C + + compiler automatically calls the class's parameter constructor T2.print (); Test t3 = Test (30);//The programmer manually invokes the constructor for object initialization T3.print ();}
Constructor and destructor Call rules:

1 when no one constructor is defined in a class, the C + + compiler provides a parameterless constructor and a copy constructor

2 the C + + compiler does not provide a parameterless constructor when any non-copy constructor is defined in the class (no arguments, no arguments)

3 the C + + compiler does not provide a parameterless constructor when a copy constructor is defined in a class

4 default copy constructor member variable simple assignment

Summary: As long as you write the constructor, then you have to use.

The function of a destructor is not to delete an object, but to do some cleanup before undoing the memory occupied by the object, so that the memory can be allocated to the new object for use by the program. Destructors do not return any values, no function types, and no function arguments. Therefore it cannot be overloaded.A class can have multiple constructors, but only one destructor. Of course, destructors can also be used to perform any operations that the user wants to perform after the last use of the object, such as output-related information. If the user does not have a destructor defined, the C + + compilation system automatically generates a destructor, and nothing actually happens.
In general, the order in which destructors are called is exactly the opposite of the order in which constructors are called:The constructor that is called first, its corresponding destructor (in the same object) is finally called, and the constructor that is called is the first to be called.
Reprint Please specify source: http://blog.csdn.net/lsh_2013/article/details/45441901

Constructors and destructors in 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.