One of the advanced C + + steps

Source: Internet
Author: User
Tags function definition


in practical work, the discovery of university C + + textbooks on the C + + knowledge is not enough, or understanding is not deep enough, this aspect also has to dash electricity. Here are a few points that you have previously not been familiar with.

1. construct and copy functions

l Assignment Constructors: T (T &)

l Assignment copy function: operator = (T &)

l To move a constructor: T (t &&)

l Move copy function:operator = (t&&)

Generally, if there are pointer members in the class, it is recommended to disable the assignment construct and the assignment copy function. Typically, for performance optimization purposes, you can use the move constructor and the move copy function to reduce redundant copies.

To illustrate:

Class T {

T (int);

T (const t&);

Operator = (const t&);

T (const t&&);

}

int main () {

t a = t (1);

T && B = t (2);

}

above During the initialization of T a , one copy is constructed at a time, and T b is constructed only once.

2. Virtual

l Limit virtual functions:virtual func (...);

l Limit pure virtual function : virtual func (...) =0, abstract class only pure virtual function

l Qualify the inherited base class as the virtual base class, avoiding the subclass having duplicate member methods in the base class;

Example: class b:virtual A;

Class C:virtual A;

Class D:class B, Class C; then the object of D is only a member of A;

l the destructor of an abstract class must be a virtual function (an individual's understanding is that the resources that may be used for the virtual function inside are uncertain)

3. use of auto inference variable type auto in C + +

the use of Auto has greatly facilitated the use of enumerators, hiding the enumeration process, for example:

Auto x1=3.1415169;

Std::map<float, int> Mymap;

for (Auto it = mymap.begin; it! = Mymap.end (); it++) {

......

}

4. Use of C + + anonymous function Lamnda

The essence is to go to the class temporary function definition when the name, through "" instead. This function is usually in the definition of a comparison function in some algorithm libraries and a temporary function that determines whether it is equal.

The format is as follows:

[Capture] (Parameters)->return-type{body}

which capture high-speed This temporary function, whether and how to use external variables, the rules are as follows:

[]        //variable not defined.tried toLambdathe use of any external variables is wrong in the.  

[x, &y]    //x   capture by value ,&NBSP;Y&NBSP; capture by reference . &NBSP;&NBSP;

[&]&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; // Any external variables that are used are implicitly captured by reference &NBSP;&NBSP;

[=]         // Span style= "font-size:12px;font-family: ' Wawati SC Regular '; color: #008200; border:none windowtext 1px;padding:0" > Any external variables used are implicitly captured by value   

[&,&NBSP;X]&NBSP;&NBSP;&NBSP;&NBSP; //x .&NBSP; Other variables captured by reference   

[=, &z] //z Capture by reference .  Other variables are captured by value  

Examples are as follows:

"" (int A, int b)->int (int z= a+b; return z);

5. The order in which constructors and destructors are executed

The specific rules are as follows:

l if the object type is global/static and so on. Data or . BSS , then for global variables, precede main () function execution constructs, and for function domain , the execution of a construct begins where it is executed. The destructor is carried out in the order of mirror symmetry;

l if the type of the object is on the stack, it is constructed at the place where the code is executed, and is then refactored at the end of the scope;

l if the type of the object is on the heap, it is executed according to the order of the destructor being explicitly called, and if the destructor is not explicitly called, the destructor is called when the heap is released by the system;

l for multiple inheritance cases, the constructor goes along the direction from the base class to the subclass, and the destructor executes in the opposite direction.



One of the advanced C + + steps

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.