C++primer the 13th Chapter

Source: Internet
Author: User

//1. When defining a class, we show or implicitly indicate that this type of object (note here is the object of this type, not including pointers of this type) copy, move, assign, and do what when destroying. A class controls these operations by defining five special member functions: Copy constructors, copy assignment operators, move constructors, move assignment operators, and destructors. //when one of the five special member functions is defined, it is generally necessary to define several other actions. //the first parameter of a copy constructor must be a reference type, and if the first argument is not a reference type, it will cause a logic error: the call to a value generates a temporary object that must be generated through the copy constructor of the class, so it is trapped in a dead loop//2. The conversion constructor for the class is called automatically. CMyString (Char*TEMSTR): Str (TEMSTR) {printf ("B0");} CMyString STR0="S0";//The transformation constructor for the class is called here//3. The assignment operator for the class should return a reference to its left operator object. The left object is returned in order to be able to assign values consecutively, and returns the reference for efficiency. //4. In a destructor, the function body is executed first, and the members are destroyed in reverse order of member initialization. When an element is destroyed in a container, the element in the container automatically executes its destructor. //destructors that point to a type are not called when a reference to an object or a pointer leaves the scope. When a dynamically assigned class-type pointer is deleted, its destructor to the type is called. //when a class needs to define a destructor, it is generally necessary to define additional copy operations. //objects of this class are not allowed to be defined when the destructor of a class is defined as inaccessible. This type of object can be allocated dynamically, but it cannot be freed. //5. Synthetic Copy control members may be deleted:A: If a member of a class has a destructor that is either deleted or inaccessible, the class's composition destructor and the composite copy constructor of the class are defined as deleted. B: If the copy constructor of a member of a class is either deleted or inaccessible, the composite copy constructor for the class is defined as deleted. C: If the copy assignment operator of a member of a class is either deleted or inaccessible, or if the class has a const or reference member, the composite copy assignment operator of the class is defined as deleted. D: If a member of a class has a destructor that is either deleted or inaccessible, or if the class has a reference member and there is no class initializer, or if the class has a const member that does not have a class initializer and the type does not specify a default constructor, the default constructor for that class is defined as deleted. Summary: If the data members of a class cannot be constructed, copied, copied, destroyed by default, then the corresponding member function will be defined as deleted. //6. Declaring a series function such as a copy constructor of a class in private, and not defining this type of function, prevents this type of object from being copied. Note: You cannot define, otherwise member functions of a class or friends of a class can still access these functions. //7. Assignment operators typically combine the operations of destructors and copy constructors. Note: If you assign an object to itself, the assignment operator must work correctly. //8. With && to get rvalue references, rvalue references have a special property: You can only bind to objects that are about to be destroyed (you cannot bind rvalue references directly to the Lvalue) to achieve the movement of resources. //the left value is persistent and the right value is short. Because the rvalue reference variable itself is an lvalue variable, you cannot bind an rvalue reference to an rvalue reference object. //You can use the Std::move function to obtain an rvalue reference that is bound to an lvalue. This function is defined in the header file utility. Declared in the namespace Std. //The move call tells the compiler that we have an lvalue, but we want to handle it like a right value. After calling move, it means committing: to the called variable, we will no longer use it except for assignment or destruction. After calling Std::move, you cannot make any assumptions about the value of the called variable. //when using move, use the following method: Std::move. This can prevent potential naming conflicts. //when we write a move operation, moving resources from an object without destroying the object, you must ensure that the source object is moved into a valid and destructor state. //since the source object has an indeterminate state after it has been moved, when we call move, we must definitely confirm that the source object has no other users after the move. Careful use of move in your code can greatly improve performance. //9. When you do not define your own move operation, the corresponding copy operation version is invoked by a function match. //The compiler defines a mobile control member only when a class does not have a copy control member defined for its own version, and every non-static member of the class can move. //when a class defines its own move operation, its corresponding default copy operation is defined as deleted. //10.make_move_iterator: Converts an ordinary iterator to a mobile iterator. The make_move_iterator is defined in the header file iterator. Declared in the namespace Std. allocator<unique_ptr<Char>>Allocstr;auto pStr= Allocstr.allocate (Ten); Vector<unique_ptr<Char>> Vecstr (Ten); Generate (Vecstr.begin (), Vecstr.end (), [] () {return(unique_ptr<Char>)New Char(0);});//uninitialized_copy (Vecint.begin (), Vecint.end (), PSTR); //This sentence code is illegal. Unique_ptr objects are non-copiedUninitialized_copy (Make_move_iterator (Vecstr.begin ()), Make_move_iterator (Vecstr.end ()), PSTR);//executes correctly, moving the VECSTR resources to the memory pointed to by PSTR. //11. Performance can also be improved if a member function provides both copy and mobile versions. //void Push_back (_ty&& _val): Accepts the Rvalue reference version to move the resource. This version is called for the push_back operation of a container that stores elements of the unique_ptr type. //void push_back (const _ty& _val): This version is a copy version. 

C++primer the 13th Chapter

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.