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

Source: Internet
Author: User

From http://blog.csdn.net/randyjiawenjie/article/details/6666937 thanks to the original author for sharing.

Class CTest
{
Public
CTest (); constructor function
CTest (const CTest &); Copy constructor
CTest & operator = (const ctest &); Assignment character
};

Ctest::ctest ()
{
cout<< "Constructor of CTest" <<endl;
}

Ctest::ctest (const CTest & Arg)
{
cout<< "Copy Constructor of CTest" <<endl;
}

CTest & ctest::operator = (const CTest & Arg)
{
cout<< "Assign function of CTest" <<endl;
}

int main ()
{
CTest A;
CTest B (a);
CTest C = A; Note that the copy constructor is still used here
A = C; Using the assignment character
return 0;
}

The results of the operation are as follows:

Constructor of CTest
Copy Constructor of CTest
Copy Constructor of CTest
Assign function of CTest

It's all very clear.

Http://hi.baidu.com/calrincalrin/blog/item/b313045023fd66998d543041.html summarized as follows:

the difference between a copy constructor and an assignment operator
a copy constructor, also known as a copy constructor, differs from the assignment operator in the following ways:
1. Conceptually differentiated:
a copy constructor is a constructor, and an assignment operator is a category of operator overloading, which is usually a member function of a class
2. Differentiate from prototypes:
copy Constructor prototype ClassType (const ClassType &); no return value
assignment operator prototype classtype& operator= (const ClassType &); Returns a reference to ClassType, facilitating continuous assignment operations
3. To differentiate from the occasion of use:
The copy constructor is used to produce the object, which is used in several places: when the function parameter is a value type for a class, when a function returns a class type, and an initialization statement, for example (it is simpler to sample initialization statements, function arguments and function return values as class value types, no example is presented here)
ClassType A; //
ClassType B (a); Call the copy constructor
ClassType C = A; Call the copy constructor
The assignment operator requires that the left and right objects of ' = ' are already present, and that the function is to assign the value of the object to the left of the ' = ' object
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 a 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 a reference to the current object without assigning a value

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.