Copy constructor and assignment operator overloading

Source: Internet
Author: User

when to define your own copy constructor:When a class contains a dynamically assigned member or pointer. If you use the default constructor, the newly constructed new class and pointer members inside the old class point to the same space, and when one of the classes empties the space. The pointer to another class becomes a wild pointer (because space has been emptied), which means that the default constructor is the copy value (the address is also the value) PS. An array of basic data types can use the default copy constructor directly. In other words, classes that involve data types and pointers that open up additional space for themselves need
when to call the copy constructor:1. An object is passed in as a value to a function PS: The parameter of the copy constructor must be a reference, because when an object parameter is passed into a function as a value, a temporary variable is created to copy the object parameter, and this is the same step. Creates a temporary variable.     The final stack overflow. When an object reference is passed in, the function can use the object directly, without creating a new object. does not cause a recursive stack overflow. (const can not.) But it's better to have. Limited to read-only, and the const variable cannot be arbitrarily converted to non-const, if the const variable is passed in, and the parameter type is not const, then the conversion error 2. An object is returned as a value from a function 3. When an object is initialized to another object Ps:te ST is the class name, Q is the copy constructor that has been initialized with the object Test K (q) and test k = q called. Because K is an undefined object when K has been defined.     The call is the = number operator Test k;    Defined first (the default constructor initializes the base type member) K = q; Invoke = operatorShallow copy:Just a simple value copy. Arrays are also valid deep copies: The pointer is pointing to it. In addition, open up new memory space and copy. Copied and copied objects. The member pointer points to the containing same value. Different memory spacecopy constructor:Disclaimer: Test (const Test & cpy) out-of-class definition: Test:: Test (const test &cpy)assignment operator OverloadingDeclaration:test& test::operate = (const Test & cpy) out-of-class definition: test& Test:: Operate = (const Test & cpy) The difference: based on whether the raw The depreciation object. If it is a build object, the copy constructor is called. If the object already has one.   Call the assignment function so the content defined by the two should also be different. The copy constructor is for an empty object. Something that has a point in the memory space should be opened up new space. The assignment function is for an already existing object, to reassign the original member (if it is old), and to delete the older (check whether it is self-assigned, free up the original space, allocate new memory resources, and copy. Returns a reference to this object)

Copy constructor and assignment operator overloading

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.