"[G]enerating a hash code from the object's address" is not
the same as "return[ing the] address of the object".
Isn't that more or less what I said in the first paragraph of my
response?
The Java function cited above basically generates a hash code
somehow from the identity of the object. Since C++ doesn't
allow moving objects around in memory, the identity is basically
the object's address, and generating a hash code from the
object's address in C++ does more or less what
System.identityHashCode( object ) does in Java.
And hashing on pointer types when someone is putting pointers
into a container is not the same as hashing on object
addresses when someone hasn't written their own hash function.
Now that seems obvious. But the only use of
System.identityHashCode() in Java is to if you've written a
class which uses == (and not Object.equals()) for equality on
some of its subobjects.
As I said (somewhere, I think), one frequent error in Java is
overriding Object.equals(), but not Object.hashCode(). To be
fair, the Java documentation is very, very clear about the need
to override Object.hashCode() whenever you override
Object.equals(). A more robust implementation of
Object.hashCode() in the base class would have been something
along the lines of:
if ( equals is overridden ) {
return 0 ;
} else {
return System.identityHashCode( this ) ;
}
But robustness has never been a particular characteristic of
Java.
(BTW: maybe I am misinterpreting your original statements. With
regards to hash codes, I tend to consider good/bad as a
different distinction than correct/incorrect, with good/bad only
applying to correct hash codes. Thus, "return 0" is a correct
hash code, but it certainly cannot be considered a good hash
code. If by "not good", you meant "incorrect given the usual
equivalence function used", and not "correct, but with a bad
distribution", then I agree.)