A brief analysis of four functions

Source: Internet
Author: User

constructor function

data members are more private, and to initialize them, they must be performed using a common function, which should be executed automatically once, and only when the object is defined.

1. Constructors and classes have the same name and no return value (no return value is not a return value of void).

2. The system is automatically called when the object is constructed (instantiated).

3. Constructors can also be defined outside the class (class name::).

4. If no constructor is given in the definition, the compiler automatically generates a default constructor, which is not automatically generated if the constructor is defined by itself.

5. Constructors with no parameter and full default values are considered to be default constructors, and the default constructor can only have one.

6. Constructors can be overloaded, the description can have more than one constructor, they are distinguished by different parameter tables, the system is automatically called by the General function overload rule selected one execution.

PS: Three functions of a constructor

1. Constructing objects

2. Initializing objects

3. Type conversion



Destructors

the process that is opposite to the constructor function

1. Add ~ Before the class name (can be understood as inverse of the constructor).

2. No parameter no return value.

3. A class has only one destructor, and if no definition is displayed, the system automatically generates a default constructor.

4. The system is called automatically when the object's life cycle ends.

5. Do cleanup work only (reclaim space, close files, etc.) instead of deleting objects.


Copy constructor

1. Initialize objects with similar objects when they are created.

2. The parameter must use a reference pass (otherwise it will cause infinite recursion).

3. Copy construction is an overload of the constructor, which is a special constructor.

4. When a function's formal parameter is an object of a class, it is used when a function is called when a form participates in an argument. This is to create a local variable in memory and copy the arguments to the new object.

5. When the function returns the value of the value class object, the function execution is completed when the caller is returned. (Create a temporary object before returning the caller).

PS, a local variable dies when it leaves its function, it is not possible to survive after returning to the calling function, so the compiler creates an unnamed temporary variable in the expression that invokes the function, and the temporary object's lifetime is only in the expression at the function call. The so-called return object is actually called the copy constructor, and the value of the object is entered into the temporary object.


Assignment statements

See the program for detailed analysis.

PS: When referencing is returned, although space and time can be saved, the reference is returned with the reference to the object being referenced, and the referenced object is not affected by the scope of the function, and if a function scope is destroyed, it cannot be referenced back.

Class test{public://constructor Test (int data=0): _data (data) {}//copy constructor Test (const test &T)//formal parameter must refer to {_data = T._data;} Assignment Statement test& operator= (const test&t)//test& operator= (test*const this,const test&t), this indicates the current object address// Referencing a parameter, without invoking the copy constructor, without creating a new space to store the temporary object, both time and space Save the//const representation as a constant reference, protecting the original object from being changed inside the function//Returning the object reference type instead of void, which can appear at the time of the call. And references can be avoided by calling copy constructors, both time and space save {if (this! = &t)//self-assignment, self-assigned value, meaningless, waste time {_data = t._data;//member corresponds}return *this;} /*void operator= (Test t) {if (This! = &t) {_data = T._data;}} If the assignment statement writes like this, the copy constructor is called first (the function's formal parameter is the object of the Class), and the assignment statement is called, inefficient, wasted space *///destructor ~test () {}private:int _data;}; int main () {test t;//constructor, constructor test T1 (10);//initialization, constructor test t2 = t1;//Initialization, object construction object, call copy constructor test t3;//test t3 (); Error, this is a function declaration, parameter is empty, no longer object t3 = t1;//t3.operator= (&t3,t1), call assignment statement//int Value;//value = t4;//Error}


This article is from the "incomparable Warm yang" blog, please be sure to keep this source http://10797127.blog.51cto.com/10787127/1759825

A brief analysis of four functions

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.