Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!microsoft!jimad From: jimad@microsoft.UUCP (Jim ADCOCK) Newsgroups: comp.lang.c++ Subject: Re: deepEquality and shallowEquality revisited. Message-ID: <71995@microsoft.UUCP> Date: 22 Apr 91 20:05:11 GMT References: <10646@orca.wv.tek.com| Reply-To: jimad@microsoft.UUCP (Jim ADCOCK) Organization: Microsoft Corp., Redmond WA Lines: 59 In article <10646@orca.wv.tek.com| steveb@tekcsc.WV.TEK.COM () writes: (quoting generally good stuff from Harry Porter) |First, "object identity" is just a pointer comparison: two objects are |identical if they are the same object. Often times, people say "two |objects are identical if they are the same object." By "the same |object," we mean they are stored in the same memory location. I disagree, rather: "An object's identity is that which distinguishes an object from all other objects." I caution C++ programmers that while defining "object identity" to be the same as a memory address works pretty well for Smalltalk, there are many problems with such a definition in C++. The reasons that using a memory address as the representation of an object's identity works well in Smalltalk include: 1) Smalltalk has garbage collection. Thus if an object no longer exists, we are guaranteed that its "identity" no longer exists anywhere in the system, thus we are free to re-assign that identity to some newly created object. 2) Smalltalk [generally] doesn't have multiple inheritence, thus avoiding the C++ problem that a MI object may have several addresses each of which can be considered as representing the object. Some examples of common "identity traps" that C++ programmers constantly fall into by equating 'this' to be the same thing as an object's 'identity.' * An embedded object may have the same address as an embedding object. * MI objects may have several significant addresses. * Compilers differ in how they lay out objects, thus these problems change with compiler. * C++ has VERY ill-defined rules about when and where compilers may make a copy of an object, changing "its 'identity.'" * Two objects may occupy the same address at different points in time, and C++ doesn't guarantee that all references to the prior object have gone away. etc, etc, etc. Thus I claim an object's address in C++ IS NOT its identity. To again quote Khoshofian and Abnous: "An object's identity is that which distiguishes an object from all other objects." Clearly, in C++, a memory address is frequently insufficient to correctly make this distinction. |The importance of getting all these concepts straight cannot be over- |emphasized. When constructing complex programs, you must know |whether objects are identical or not. Agreed. Thus I warn C++ programmers that frequently it is inappropriate to consider a C++ object's address[es] to be its "identity." For more complete discussions see: "Is 'this' identity?" in Feb 1991 'The C++ Report' and Object Orienteering by Khoshafian and Abnous