C + + Primer 13.1: Copying, assignment, and destruction

Source: Internet
Author: User

1. Copy constructor basic form: Name (const name&) for class-type members, copies of the copy constructor are called and copied directly for the built-in type members.    For arrays, the synthesized copy constructor is copied one by one in the other array (built-in type). When the copy constructor occurs:
    • Occurs when a variable is defined with =
    • To pass an object as an argument to a parameter of a non-reference type
    • Returns an object from a function with a return type of non-potable type
    • Initializes an element in an array or a member of an aggregation class with a list of curly braces
Note: The first parameter of the copy constructor must be a reference to itself, because the copy constructor is used to initialize the non-reference class type parameter, and if the argument is not a reference to itself, then the success will not be invoked and an infinite loop. In order to invoke the copy constructor, the arguments are copied, and the copy constructor is called. 2. Copy assignment operator (when present on Assignment) the basic form:name& operator = (const name&) should normally return a reference to the left-hand operand. The right non-static object is assigned to the left member. The synthetic Copy assignment operator assigns array elements (built-in types) one by one.  3. The destructor frees the resource used by the object and destroys the object's non-static data members. When the destructor occurs:
    • The variable is destroyed when it leaves its scope
    • When an object is destroyed, its members are destroyed
    • When a container is destroyed, its elements are destroyed (including arrays)
    • Dynamically allocated object that is destroyed when the delete operator is applied to a pointer to it
    • A temporary object that is destroyed at the end of the complete expression that created it (scope)
Note: The destructor itself does not destroy the member, which is destroyed in the implicit destruction phase after the destructor body. During the entire object destruction process, the destructor body is performed as part of the member destruction step.  4.35 Law 1. Classes that require destructors require copy and assignment Operations 2. A class that requires a copy operation also requires an assignment of 5. Using = default can be defined in the Copy control member as follows: Name () = default;  Default constructors Name (const name&) = default;  name& operator = (const name&) = default;  ~name () = default; To display instructions using the compiler composite version.  6. Use = delete can also be defined as follows: Name () = delete;  Default constructor Name (const name&) = delete;  name& operator = (const name&) = delete; Destructors cannot be declared as being deleted to illustrate blocked copy assignment operations (old standards can put these into private block copies). For some classes, the copy assignment operation is meaningless. Note: = delete must appear when the function is first declared, and = default is not required until the compiler generates the code. You can also use = delete for any other function. 7. Synthetic copy control members may be deleted if the following occurs, the synthesized copy control member is defined as delete:
    • If a destructor for a member of a class is either deleted or inaccessible, the destructor is defined as delete
    • If the copy constructor for a member of a class is either deleted or inaccessible, the composite copy constructor for the class is defined as deleted.
    • If a destructor for a member of a class is deleted or cannot be accessed, the composite copy constructor for the class is also defined as deleted.
    • If the copy assignment operator of a member of a class is either deleted or inaccessible, or the class has a const or reference member, the composite copy assignment operator of the class is defined as deleted.
    • If the destructor for a member of a class is either deleted or inaccessible, or the class has a reference member, it does not have a class initializer, or the class has a const member, it does not have a class initializer, and its type does not display the definition default constructor, the default constructor for that class is defined as deleted.

C + + Primer 13.1: Copying, assignment, and destruction

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.