The difference between a C + + copy constructor and an assignment

Source: Internet
Author: User

differences between C + + copy constructors and evaluators:http://blog.csdn.net/randyjiawenjie/article/details/6666937


copy constructor and assignment function :http://www.cnblogs.com/dwdxdy/archive/2012/07/17/2594993.html



!!!! The above two articles speak better, it is worth to look carefully.



The difference between a copy constructor and an assignment operator
A copy constructor is also called a copy constructor, and the difference between it and the assignment operator is reflected in the following aspects
1. Conceptually distinguished: The
copy constructor is a constructor, and the assignment operator is a category of operator overloading, which is usually the member function of a class
2. Differentiate from prototype:
Copy constructor prototype ClassType (const ClassType &), no return value
Assignment operator prototype classtype& operator= (const ClassType &) ; The return value is a reference to ClassType, facilitating continuous assignment of operations
3. Differentiate from usage: The
copy constructor is used to produce the object, which is used in the following places: When the function parameter is a value type for a class, when the function returns a class type, and an initialization statement, for example, the initialization statement, the function argument and the function return value as the value type of the class, is not shown here.
ClassType A;        //
ClassType B (a);    //Call copy constructor
classtype C = a ;    //Call the copy constructor
and
ClassType E;
Class Type F;
F = e;              //call assignment operator
4. When a class contains a pointer member, the meaning of the two is very different. The
copy constructor allocates memory space for the pointer variable and copies the value of the argument to it, while the assignment operator implements the function to copy the value to the left value to the right of the ' = ' sign, and then to release and then apply when the left object is low on memory. Of course, the assignment operator must detect if it is itself assigned, and if it returns the current object's reference without assigning an action



The difference between a C + + copy constructor and an assignment

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.