Thinking again in C ++ (2) the auto-assigned value is non-public disconnected.

Source: Internet
Author: User

I love thinking in series, so I started this name. The idea of this article is also part of this set of books, for example, comparison, or in-depth mining, or to make up for wisdom, or to make sense, including thinking in C ++, it even includes thinking in Java.

Thinking again in C ++ (2) the auto-assigned value is non-public disconnected.

Keywords: C ++, auto-assigned, auto-copied, assigned, assign, assignment, copy, copy

1. Self-assignment to be considered. Check whether a class contains pointers or reference members.
Class string
Char * pc_buffer;
String & operator = (const string & strr );
String & operator ++ = (const string & strr );
(1) internal class: symmetric value assignment operator, member functions that accept parameters of their own type or their own base class type, and sometimes + = series operators must be considered.
String & string: Operator = (const string & strr)
If (this = & strr) // [1]
Return * this;
Delete [] pc_buffer; // [2]
Pc_buffer = new char [strlen (strr. pc_buffer) + 1]; // [3]
The judgment in [1] is required. If this = & strr, [2] will delete itself, [3] will use "hanging pointer ".
The following operator ++ = () Implementation hides errors.
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] will delete itself, [5] will use "hanging pointer ". If the statement is correct, you do not need to use a judgment statement. Instead, you only need to change the order of the two statements [4] [5.
(2) Class Exterior (including youyuan): a function that accepts multiple parameters of the same type or multiple parameters of the inherited type.
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] All have self-assigned values. Therefore, F () and g () must be considered in the design.

2. Auto-assigned values cannot appear.
(1) copy constructor: because the object being constructed has not yet been fully generated, the real parameter object passed to the constructor is a constructed object, and the two cannot be the same object.
(2) asymmetric value assignment operator: even if the parameter type is its own base class. If D is a derived class of B, no matter whether or not the symmetric assignment operator is overloaded, the assignment between Class D Objects will not call D: Operator = (const B & B ).
Class cderive: Public cbase
Operator = (const cbase & B); // you do not need to consider the auto-assigned value between this and B.
Void F (const cbase & B); // you need to consider the auto-assigned value between this and B.
Cderive dsame;
Dsame = dsame; // [1]
Dsame. F (dsame); // [2]
In statement [1], the compiler does not document the dsame as cbase, but calls the default or custom D: Operator = (const D & D ). D: Operator = (const B & B) is called only when the equation is D on the left and B on the right. In this case, auto-assignment is impossible. On the contrary, in statement [2], the compiler will shape the dsame as cbase, so f () needs to consider the auto-assigned value.

3. It is not an auto-assigned value. Only values with the same content are not self-assigned values.
Ctest a, B, same;
A = same;
B = same;
A = B; // [1]
[1] It is not a self-assigned value, and there is no problem. You do not need to check the content, and you cannot directly use the address to check the content.

4. the auto-assigned values should not be checked.
In strcpy (char * strdest, const char * strsrc);, when strdest = strsrc, it is a self-assigned value, but no error occurs.
Direct return of self-assigned values may increase the function efficiency by 10 times in certain cases, but the vast majority do not have a conditional judgment when self-assigned values appear, which may reduce the function efficiency by 10%, at last, the weighted average efficiency of the comprehensive calculation may be reduced. This depends on the probability of auto-assigned values.
If you do not judge the auto-assigned value, the function execution time is 1. If you check the auto-assigned value, set the probability of the auto-assigned value to X, and directly return the function execution time to 0.1, without the auto-assigned value, if a condition is added to determine that the function execution time is 1.1, the weighted average efficiency is not reduced:
0.1x + 1.1 () <1
Resolution: x> 0.1. That is to say, the probability of auto-assignment must be greater than 10%. Is this possible in actual code?

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: 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.