TITLE: hashing, templates, and inheritance PROBLEM: Michael Bresnahan I was was going to implement a associative array container template class using a hash table. After thinking about it a bit, I discovered their was a problem. How does one write an efficient template hash function that will work for any type? RESPONSE: rmartin@rcmcon.com (Robert Martin), 26 Sept 95 Probably not a good idea since you only want to hash the "key" of the object (if that term makes any sense). Some libraries force you to inherit from some class (e.g. Hashable) that has a pure virtual int hash() function declared in it. Any object that you want to put into the hash table must be derived from Hashable. I find this approach limiting. I do not want to derive objects from Hashable. Moreover, I may be putting objects of some third party class into the hash table, and so am not able to derive from Hashable. My favorite solution is to use overloaded global functions. template void AssociativeArray::Add(T& t, U& u) { ... int hashcode = Hash(t); } int Hash(int); // declare function for hashing ints. int Hash(double); // declare function for hashing doubles. int Hash(Blurb*); // declare function for hashing Blurbs. AssociativeArray aa; // will call Hash(int); AssociativeArray ds; // will call Hash(double); AssociativeArray bs; // will call Hash(Blurb*);