TITLE: Comparing against "this" PROBLEM: mod@cs.man.ac.uk (Mike O'Docherty) According to Scott Meyers in "Effective C++", checking two pointers for equality (i.e. object *identity* rather than value) only works for *single* inheritance: "The minute you start to use multiple inheritance, you must steel yourself for the fact that an object can have more than one address..." The implication is that C& C::append(const C& rhs) { if (this == rhs) { // do something... } else // do something else } } is not safe because `&rhs' may point to a *different* part of the *same* object. However, the FAQ shows the above test being used to short-circuit a copy constructor (Q83). Obviously, I can't just assume single inheritance. (1) Who's correct? [...] RESPONSE: jimad@microsoft.com (Jim Adcock), 20 Apr 94 Scott's statement is correct, and the FAQ is clearly wrong, which is not to imply the two statements are exact opposites of each other. It is clearly wrong to short-circuit a copy constructor because that would imply that it is legal to invoke a constructor to create an object on top of a non-destructed object, but that is always illegal. Thus the short-circuit makes no sense in the copy ctor case. It can make sense in the assignment operator case, however, since it is in general legal and sensible to assign an object to itself. However, whether a general case like your "append" makes sense or not depends on *your* rules and object models -- C++ contains no such rules and detailed object models. Does it make sense for an object to contain two distinct subobjects of the same type? Depends on what you are doing. Does it make sense to append from one subobject to the other? Depends on what you are doing. You make the models, and you make the rules. [...] [ Moral of story: - don't try to protect the copy constructor against copy-constructing on itself, and - be careful about assumptions of object addresses with multiple inheritance. - adc ]