Effective C + + 51 cases where copy assigment operations are not generated automatically

Source: Internet
Author: User

The main talk

1. In general, the compiler creates default constructors for classes, copy constructors, and copy assignment functions

2. When performing the default copy construction/copy assignment function, if the member has its own copy Construction/copy assignment function executes it, otherwise it will be copied/assigned by bit

3. Several compilers do not generate a default copy assigment operation for a class

These situations are as follows:

A. The corresponding constructor, copy constructor, copy assignment function are already in the class. Generating the default functions at this point loses its original meaning.

B. Other cases are due to some of the steps in 2 that cannot be performed or if execution violates some of the C + + design specifications

B.1 the copy asignment function for some members (or parent class) is private, causing the copy step to fail, and the default function does not generate a

classAge { Public: Age (int_age): Age (_age) {}Private:        intAge ; Age&operator=(Constage&RHS) {            if(&RHS! = This) {                 This->age =Rhs.age; }            return* This; }};classMan {Private: age age; Public: Man (int_age): Age (_age) {}}; Mans Man1 ( A); Mans Man2 ( A); Man1= Man2;

Compiler hints:

[Note] ' man& man::operator= (const man&) ' is implicitly deleted because the default definition would be ill-formed:

[Error] ' age& age::operator= (const age&) ' is private

Some of the members of the B.2 class are const or reference types, and we know that the const variable cannot be assignment after the assignment, and the reference type variable is the same (the object pointed to cannot be changed)

#include <iostream>#include<cstdlib>#include<string>using namespacestd;classNamedobject { Public:    string&name; int&Age ; Namedobject (string& _name,int&_age): Name (_name), age (_age) {}};intMain () {stringN1 ("obj1"); intA1 =123; stringN2 ("Obj2"); intA2 =321;        Namedobject obj1 (N1, A1);        Namedobject Obj2 (N2, A2); Obj1=Obj2; return 0;}

Obj1 = Obj2, which is equivalent to re-assigning a const variable and changing the reference point to the object, is obviously not supported, so the compiler cannot create the default behavior of the copy assignment function, but must be handled by itself.

Effective C + + 51 cases where copy assigment operations are not generated automatically

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.