C ++ constructor and analysis function

Source: Internet
Author: User

Constructor Concept

(1) The constructor isSpecial member functions

(2) When creating a new object of the class type, the system automatically calls the constructor.

(3) constructor is used to ensure that every data member of the object is correctly initialized.

Precautions for creating constructor:

(1) The function name is the same as the class name;

(2) If no return type exists, the return type cannot be void.

(3) The constructor is usually declared as public. Otherwise, it cannot be called as shown in other member functions.

(4) constructor can also be declared as private, but it is used for special purposes. For example, if singleton is a singleton class, the constructor is declared as private.

(5) constructor can have any type and any number of parameters. A class can define multiple Constructor (reload)


Default constructor:

The default constructor is one of the constructor types. In addition, there are also copy constructor and conversion constructor, which will be explained later. The default constructor is a constructor without any parameters. If no constructor is defined in the program, the system automatically generates a default constructor, in this case, the object data members are not initialized, so the values of the object data members are random. If we provide a constructor (as long as it is a constructor, whether it is a constructor with parameters or without parameters, whether it is a copy or a conversion constructor ), even if only one constructor is provided, the system will no longer provide default constructor for us.

The following is the class definition of the constructor:

Class Test {public: Test () {}// the system no longer provides the default constructor Test (int val) // The constructor can overload {val _ = val ;}~ Test () {} private: int val _;};
The following code uses the Test class:

Int main () {Test t1; // default constructor Test t2 (5) without parameters for system calls ); // The system calls the constructor with a parameter Test * t3 = new Test (20); // allocates memory + calls the constructor with a parameter. This operation is called new operator. delete t3; // call destructor + release memory}

Note: The constructor of the global object is executed before the main function.

Destructor Concept

(1) The function name is similar to the class name. "~ "Symbol (Anti-operator)

(2) No return type

(3) parameters are not allowed.

(4) cannot be overloaded (this is easy to understand, because there is no parameter, it is naturally not reloaded)

(5) If no destructor is defined, the compiler automatically generates a default destructor. The function body is empty.


Destructor and object Array

To grasp a principle and construct several objects, we need to call several destructor.

Take the Test class above for example:

Int main () {Test t [2] = {10, 20}; // initialize the object array and create two elements, that is, two Test objects, respectively passing the initial values of 10, 20, that is to say, a constructor with a parameter is called. Because two objects are created, the constructor also calls Test * t2 = new Test (2) twice. // only one object is created. The initial value is 2, call a constructor with a parameter. Delete t2; // call the Destructor once. Test * t3 = new Test [2]; // create two objects on the stack without an initial value. The default constructor without parameters is called. Because two objects exist, therefore, it was called twice. Delete [] t3; // call the Destructor twice and release the memory .}

Note: destructor can be explicitly called. However, when the object lifecycle ends, the system will call the Destructor again, in this way, the Destructor is called twice. If the Destructor contains the delete operation, the problem may occur. Therefore, the number of explicit calls to destructor is relatively small. Explicit calling of destructor is used in STL source code.

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.