GURU of The Week Terms 11: Object Identity

zhaozj2021-02-08  432

Gotw # 11 Object Identity

Author: Herb Sutter

Translation: Kingofark

[Declaration]: This article takes the Guru of The Week column on www.gotw.ca website, and its copyright belongs to the original person. Translator Kingofark translated this article without the consent of the original person. This translation is only for self-study and reference, please read this article, do not reprint, spread this translation; people download this translation content, please delete its backup immediately after reading. Translator Kingofark is not responsible for people who violate the above two principles. This declaration.

Revision 1.0

GURU of The Week Terms 11: Object Identity

Difficulty: 5/10

("Who is I?" This problem contains how to determine if the two pointers really point to the same object?

[problem]

Test statements "this! = & = & 中er" is a common coding technique for preventing self-assingment. So, to reach this "Self-Assingment" purpose, is the existing statement is necessary and / or sufficient? why? Or why? If the answer is negative, how do you modify it? Note that "Protecting Against Murphy vs. Protecting Against Machiavelli" is to be distinguished. (* Translation: See the end of this article)

T & T :: Operator = (const t & other) {

IF (this! = & Other) {// the test in question

// ...

}

RETURN * THIS;

}

[answer]

A short answer: From a technical point of view, it is neither necessary. In practice, it works quite good but may also be modified in C standards.

* Argument: Abnormal - Safety (Murphy)

If Operator = () is an exception-safe (Exception-Safe), then you don't need to check the self-assignment. There are two efficient factors here: a) If you can perform a self-assignment check, it can be thoroughly optimized to omit the assignment operation; b) If the code is written as an abnormally - secure (Exception) -safe), then also makes the code part of the efficiency (this is said "ParaNoia Has A Price Principle", the principle of "paranoid also has the principle").

* Is not the argument: multiple inheritance

In the past, some people have linked multiple inheritances with the issues discussed with this Territory, but in fact this problem has no relationship with multiple inheritance. Our discussion is a technical issue involving the C standard how to compare two pointers.

* Argument: Operator's overload (Machiavelli)

Although some classes may provide their own Operator & (), but the problem is likely to operate as the self-assignment, but do something completely different. It is because we can only speculate on whether there is Operator & () that we can only speculate in the writing of Operator = (). To pay attention, a class may also provide a T :: Operator! = (), But this is not related to this issue - it does not affect our inspection of self-assignment, because one is heavy At least one type of operator must be a "class" type, so we can't write a T :: Operator! = () Containing two T * type parameters.

Postmark 1

Here is a so-called "Code Joke". Letter do not believe in you, there is really some un malle, but it is undoubtedly misleaded code writer once attempted to use this code:

T :: T (const t & other) {

IF (this! = & other) {

// ...

}

}

how about it? Can you see the problem in your first?

Postscript 2

It is worth noting that there is another situation, in which these circumstances, the comparison of the pointers can not consider thoughtfulness. such as:

1. James Kanze points out that the behavior of comparing the pointer is undefined. The reason (I haven't seen someone) is a C standard clearly stipulate that the compiler is allowed to store strings in overlapping memory area as a spatial optimization scheme.

2. In general, although the result of computational operations such as <, <=,> and> = built-in) operators are in various specific cases (such as the two pointing objects in the same array) It has a good definition, but you can't compare any pointer with these operators. The use of standard libraries is also based on this restriction, which specifies: Less <> and other library functions must give the order of each pointer, so that we can create, for example, a key is MAP of the pointer type. Map >. (Translation: The MAP here is part of the STL, one map includes two parts: KEY and VALUE, when you need #include )

(* Translation: About Murphy and Machiavelli, Mr. Houjie mentioned in the "C / OOP" of its series of books: "As far as my English is, [SUTTER99] (ie" Exceptional C ") is not read If [Meyers96] (ie "More Effective C ") and [Meyers98] (ie "Effective C 2 / E") are all smooth, the reason is that there is a lot of piligers, speaking, allusion. For example, Morphy What is LAW, do you know? (Moffee Law said: Will be wrong, will be wrong.) Machiavelli also mean in what it means? (Italy politician, known as a scam, "here, the translator of this clause Kingofark can only say that Kingofark does not fully understand this Terms Use Murphy and Machiavelli's intentions.

Machiavelli: Macquasville, Nixer 1469-1527 Italy Political Theory The emerging bourgeoisie ideological politician, historian. )

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

New Post(0)