Self-assignment is non-break

xiaoxiao2021-03-06  74

Love the Thinking in series, so I played this name. The idea of ​​this article also comes to this book, or refers to comparison, or in-depth mining, or makes picking out, or has a feeling, including Thinking In C , and even including Thinking in Java.

Thinking Again In C (2) self-assignment is non-break

Keywords: C , self-assignment, self-copy, assignment, assign, assignment, copy, copy, copy

1. You need to consider the self-assignment. Check if the class contains a pointer or reference member. Class string {private: char * pc_buffer; public: string & operator = (const string & strr); string & operator = (const string & strr); // ...}; (1) Interior: symmetric assignment operator , Accept the member function of the type of self-type or its own base class, sometimes considering the = series operator. String & string :: Operator = (const string & strr) {if (this == & strr) // [1] return * this; delete [] pc_buffer; // [2] pc_buffer = new char [strr.pc_buffer ) 1]; // [3] // ...} [1] is necessary. If this == & strr, [2] deletes itself, [3] will use "hanging pointer". The following Operator = () is hidden. String & String :: operator = (const String & strR) {int iLengthNew = strlen (pc_Buffer) strlen (strR.pc_Buffer); char * pcBufferNew = new char [iLengthNew 1]; strcpy (pcBufferNew, pc_Buffer); delete [ ] pc_buffer; // [4] strcat (pcbuffernew, strr.pc_buffer); // [5] pc_buffer = pcbuffernew; return * this;} If this == & strr, [4] delete itself, [5] will be used "Hanging pointer". The correct approach does not have to use the judgment statement, simply switch [4] [5] two statements. (2) Outside (including Friends): Accept a function of multiple same type parameters or multiple type parameters with inheritance relationships. Class CDERIVE: PUBLIC CBASE {}; Void F (CBase & B1, CBase & B2); Void G (CBase & B, Cderive & D); CBase Bsame; CDerive Dsame; F (BSame, BSame); // [1] f (dsame, dsame); // [2] g (dsame, dsame); // [3] [1] [2] [3] has a self-assignment, so f (), g () design It must be considered.

2. You can't have a self-assignment. (1) Copy constructor: Because the object being constructed is not fully generated, the actor that is transmitted to the constructor is an object that has been constructed, and both will never be the same object. (2) Asymmetric assignment operators: Even if the type is the base class. If D is the derived class of B, whether or not the symmetric assignment operator is heavy, the assignment behavior between the D :: Operator = (Const B & B) is called between D :: Operator = (Const B). Class CDERIVE: PUBLIC CBASE {public: Operator = (const cbase & b); // Do not consider self-assignment VOID F (Const CBase & B) between THIS and B; // Need to consider self-assignment between this and B }; Cderive dsame; dsame = dsame; // [1] DSAME.F (dsame); // [2] statement [1], the compiler does not touch the DSAME to CBASE, but calls default or self Defined D :: Operator = (Const D & D). Only the equation is indeed d, right is indeed B, only call D :: Operator = (Const B & B), it is impossible to have self-assignment. Instead, in the statement [2], the compiler will touch the DSAME to CBASE, so F () needs to consider self-assignment. 3. Not the assignment of self-assignment. Only the same assignment is not self-assigned. CTest A, B, Same; A = Same; B = Same; A = B; // [1] [1] is not self-assignment, no problem, does not need to be checked, and the content cannot be inspected directly with the address.

4. You should not check the self-assignment. STRCPY (CHAR * STRDEST, CONST Char * STRSRC); When strDest == strsrc is self-assigned, it is not wrong. It is found that the self-assignment is directly returned. In this particular case, it may increase the function efficiency 10 times, but there is no more conditional judgment when there is no self-assignment, which may reduce the efficiency of the function, and finally compute the weighting average efficiency. Still reduced. This depends on the probability of self-assignment. If you do not judge the self-assignment, the function execution time is 1; if you check the self-assignment, set the probability of self-assignment to X, the direct return function execution time is 0.1, no self-assignment, there is more ways to determine the function of 1.1, Then, if the weighted average efficiency does not reduce: 0.1x 1.1 (1-x) <1 solution, obtained: x> 0.1. That is to say, the probability of self-assignment must be greater than 10%, is this possible in the actual code?

转载请注明原文地址:https://www.9cbs.com/read-121471.html

New Post(0)