C ++-copy constructor, copy-assignment operator, destructor, assignmentoperator

Source: Internet
Author: User

C ++-copy constructor, copy-assignment operator, destructor, assignmentoperator

For a class, copy constructor, copy-assignment operator, move constructor, move-assignment operator, and destructor are collectively referred to as copy control.

Let's talk about copy constructor and copy-assignment operator's destructor.

 

Copy constructor

Copy constructor:A constructor is a copy constructor if its first parameter is a reference to the class and other parameters have default values.

1. The first parameter must be of the reference type, because when we pass an object as a parameter to a non-reference variable of a method, the copy constructor method is automatically called, if the copy constructor parameter is of a non-reference type, this method will cause infinite recursive calls, and then your program will be boomshakalaka ~~.

2. We usually set the first parameter to const, because it will not be modified unless you have other plans.

3. Because copy constructor is called by default in many cases, copy constructor is generally not set to explicit in the following cases.

1 std: string s; 2 std: string s1 = s; // copy constructor3 std: string s2 = std: string (s1) is implicitly called ); // copy constructor is explicitly called.

 

1 class Foo{2 public:3     Foo(const Foo&);4     //...5 };

When copy constructor is called

To solve this problem, we need to understand another set of concepts: direct initialization and copy initialization.

Direct initialization: requires the compiler to select the method to be called based on the general function matching.

Copy initialization: requires the compiler to copy the right operand to the left operand. If necessary, type conversion will be performed. This process will call copy constructor or move constructor (this article will not be introduced for the time being ).

1 std::string s1("balabala");                       //direct initialization2 std::string s2(10, 'a');                          //direct initialization3 std::string s3 = s2;                              //copy initialization4 std::string s4 = std::string(s3);                 //copy initialization5 std::string s5 = "const char* converts to string";//copy initialization

Copy initialization occurs as follows:

1. Use = to initialize the defined variable.

2. When an object is passed as a parameter to a non-reference variable of a method.

3. When the method returns an object of non-reference type. (A temporary object is first generated when a request is returned)

4. When initializing an array or aggregate class member with the curly braces list.

Misunderstanding caused by the compiler:

The current compiler sometimes automatically bypasses copy constructor, that is, the compiler will

Std: Book book = "9-9-9-9"; // assume that Book is a custom class.

Replace it with the following

Std: Book book ("9-9-9-9 ");

Note that the two statements on execution are completely different. The first statement will first call the Book (const char *) constructor to generate a temporary object and then call Book (const Book &) copy a temporary object to book. In the second sentence, the Book (const char *) will be called directly before it is finished. If you want to verify their differences, you can implement the Book class and set Book (const Book &) as a private method (to prevent automatic Compiler Optimization ), then you will find that the first statement cannot be executed.

 

Copy-assignment operator

Copy-assignment operator:This method is used to reload the = Operator.

1. the return value of copy-assignment operator is generally a reference to its left-hand operand (left-hand operand), which is determined to make the object behavior more like the built-in type.

1 class Foo{2 public:3     Foo& operator=(const Foo&);4     //...5 };

When is a copy-assignment operator call?

The answer is obviously when the = Operator is used, but note that

The copy-assignment operator is not called during initialization.

The copy-assignment operator is not called during initialization.

The copy-assignment operator is not called during initialization.

The important thing is described three times, for example:

1 std: string s; 2 std: string s1 = s; // initialize s1 and call copy constructor3 s1 = s; // assign values to s1, copy-assignment operator

 

Destructor

Destructor:The destructor has two parts: function body and destruction part. The former is written by the class creator to specify the content to be done, and the latter is implicit and does not need to be concerned by programmers, when the function body is executed automatically, non-static data members of the class are destroyed.

1. Because Destructor does not have a parameter, it cannot be overloaded.

1 class Foo{2 public:3     ~Foo();4     //...5 };

When Destructor is called

1. When the scope of the object is exceeded.

2. When the container is destroyed (container), the elements in the container will also be destroyed by calling its own destructor.

3. Use delete manually.

4. The temporary variable created by an expression will automatically call destructor after the expression is executed to destroy it.

5. If a class member has a destructor, the class member will call its own destructor when the class is destroyed.

 

Synthesized)

Synthesized copy constructor:Even if we provide other versions of copy constructor, the compiler will still provide this version of copy constructor to us. It will copy non-static members to the created object in sequence, the array can also work normally, and their own copy constructor will be called for the class type.

Synthesized copy-assignment operator:The behavior is similar to Synthesized copy constructor. The non-static member is copied to the left operand in sequence.

Synthesized destructor:The function body of the destructor is empty.

 

When do we need to customize the above three methods?

1. When destructor is required, all the above three methods are required.

2. When copy constructor is required, copy-assignment operator is also required, and vice versa.

Destructor is used when we need to delete our dynamically allocated memory.

When we need to perform deep replication, we will use the other two, such as copying the elements pointed to by the pointer.

 

Usage of delete and default

We can use the default display statement to declare that we want to use the default version of copy control, or use the delete display statement. We do not need this method to prohibit this object from copying and assigning values.

1. We can delete all methods except destructor to display and inform this object that relevant operations cannot be performed. delete can only be written in a declaration.

2. We can use default to display and declare all functions with default versions. We need this default version. default can be written in the method declaration or in the method definition.

1 class Foo {2 public: 3 Foo () = default; // explicitly describe the default version 4 Foo (const Foo &) = delete; // delete copy constructor5 Foo & operator = (const Foo &) = delete; // delete copy-assignment operator6 ~ Foo () = default; // The default version 7 void myFuntion () = delete; // delete Method 8 //... 9 };

 

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.