C + + class copy, assignment and destruction (copy constructor, copy assignment operator destructor)

Source: Internet
Author: User

Copy constructor

If the first parameter of a constructor is a reference to its own class type, and any additional arguments have default values, this constructor is a copy constructor.

Copy constructor The first argument must be a reference type. This parameter is almost always a const reference. Copy constructors are implicitly used in several cases. Therefore, the copy constructor should not normally be explicit.

Synthetic copy Constructors

Unlike the composition default constructor, even if we define other constructors, the compiler will synthesize a copy constructor for us.

For some classes, the synthetic copy constructor is used to prevent us from copying objects of that class type. In general, the synthesized copy constructor copies the members of its arguments one by one into the object being created. The type of each member determines how it is copied.

Copy Initialization

The difference between direct initialization and copy initialization.

String dots (10, ', '); Direct initialization

string s (dots); Direct initialization

string s2 = dots; Copy initialization

When using direct initialization, we are actually asking the compiler to use normal function matching to select the constructor that best matches the parameters we provide. When we use copy initialization, we ask the compiler to copy the right operand to the object being created and, if necessary, to convert the type.

Copy initialization is usually done using a copy constructor. Copy initialization is done by copying the constructor or moving the constructor function.

Copy initialization takes place not only when we define a variable with =, but also in the following cases

? pass an object as an argument to a parameter of a non-reference type.

? Returns an object from a function that returns a type other than a reference type.

? Initializes an element in an array or a member of an aggregation class with a list of curly braces.

parameters and return values

The copy constructor is used to initialize the non-reference class type parameter, which explains why the copy constructor's own arguments must be reference types. If its argument is not a reference type, the call will never succeed-in order to invoke the copy constructor, we must copy its arguments, but in order to copy the arguments, we must call the copy constructor, so Infinite loop.

limitations of Copy initialization

Vector<int> v1 (10); Direct initialization

vector<int> V1 = 10; Error: The constructor that accepts the size parameter is explicit

If we want to use a explicit constructor, we have to use it explicitly:

void f (vector<int>); The parameters of F are copied and initialized.

f (10); Error: Cannot copy an argument with a explicit constructor

F (vector<int> (10)); Correct: Constructs a temporary vector from an int directly

If we want to use a explicit constructor, we have to use it explicitly:

The compiler can bypass the copy constructor

The compiler is allowed to put the following code string null_book = "9-999-99999-9";

Writes a string Null_book ("9-999-99999-9");//The compiler skips over the copy constructor.

Copy assignment operator

class controls how its objects are assigned by copying an assignment operator.

Overloaded assignment Operators

The overloaded assignment operator is essentially a function whose name consists of the operator keyword followed by the symbol that represents the operator to be defined. Therefore, the assignment operator is a function named operator=. Similar to any other function, an operator function has a return type and a list of parameters.

It is important to note that the standard library typically requires a type that is stored in a container to have an assignment operator, and its return value is a reference to the left operand.

Synthetic copy assignment operator

As with the copy constructor, if a class does not define its own copy assignment operator, the compiler generates a synthetic copy assignment operator for him. Like a copy constructor, for some classes, the synthetic copy construction operator is used to disallow assignment of objects of that type.

Destructors

Rule three/Five

Using =default

Block copy

C + + class copy, assignment and destruction (copy constructor, copy assignment operator destructor)

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.