tags:

views:

85

answers:

2

In java, if I want generate a id for a given object, the easyest way I know is to use apache commons:

public class Person {
   String name;
   int age;
   boolean smoker;
   ...

   public int hashCode() {
     // you pick a hard-coded, randomly chosen, non-zero, odd number
     // ideally different for each class
     return new HashCodeBuilder(17, 37).
       append(name).
       append(age).
       append(smoker).
       toHashCode();
   }
 }

But, how can do that in C++ ?

+5  A: 

Use boost::hash_combine.

Martin v. Löwis
Thank you, Guru! That's exactlly what I need!
wupher
A: 

By the way, the hashCode method does not return an identifier for an object. This is a common misconception. There is nothing to prevent 2 objects of the same class returning the same value. The hashCode is meant for hash table data structures, not for identifying objects. Those are 2 separate concepts.

John O
yeah, I see. When two objects' value are equal, they should get same hashcode. And they may be two different object instance. If I want get an object id, maybe UUID is a choise?
wupher