Question: I am new to computer science and I guess this has something to do with hash function.

I have a class in cpp:

```
class Car{
    public:
    int fleetId;
    OwnershipEnum owner;
    int buildDate;
    bool hasFourDoors;

    ... with many other members
};

```

Now, my problem is, how to 'scientifically' generate an integer key based on the 4 members I list?

I can think of a simple way, `10000*int(fleetId) + 1000 * int(owner) + 100×int(buildDate) + int(hasFourDoors)`

I think ideally the key is continuous, so I can store all the Car objects in an array and use this generated key to DIRECTLY access the car object.

\*\*\*\*\* as per the comments: the cars are all different, there is no identical cars \*\*\*\*\*\*\*\*

\*\*\*\*\* these four members are static: they won't change after being created \*\*\*\*\*

Can anyone point me to the right solution here?

Thanks

Answer: 
The combination of these members is not enough to uniquely identify a car: Imagine two cars having the same fleetId (because of being part of the same fleet), the same owner, and both being booked and in good condition. This would result in the same id if you calculate it based on these properties. As suggested by @jsheeran, you can use the license plate as an unique identifier. 

Otherwise, add another member for id to your car class and have a static variable for a global, increasing key on every constructor call of car. However, be aware that this is only a sample implementation and you might want to have another class for managing the keys, because this implementation will never "free" keys. 

```
class Car{

public:
    Car() : id(++nextId) {}

    const unsigned id;

private:
    static unsigned nextId;
};

unsigned Car::nextId = 0U; // initialize in your cpp using the cars

int main()
{
    Car a;
    Car b;
    Car c;

    std::cout << a.id << b.id << c.id; // will print 012
}

```

Another suggestion would be to use GUIDs. Depending on your platform or used libraries, you can find a solution here for example: <https://msdn.microsoft.com/en-us/library/ms688568(VS.85).aspx>
Let assume you want a key without collision and all field can take any value in their range, you'd have to use a sizeof(int) + sizeof(OwnershipEnum) + 2 bits key to prevent all collisions, or 66 bits. It can be considered acceptable and computed like this:

```
struct key {
    int fleetId;
    OwnershipEnum owner;
    char bools;

    key(Car _myCar) : 
        fleetId(_myCar.fleetId), 
        owner(_myCar.owner), 
        bools(!!_myCar.isGoodCondition & !!_myCar.isBooked << 1 ) 
    {}

    bool operator ==(key other) { return fleetId == other.fleetId && owner == other.owner && bools == other.bools; }
};

```

then test like this:

```
  Car first{1, (OwnershipEnum)2, false, true};
  Car second{1, (OwnershipEnum)2, false, true};
  Car third{8, (OwnershipEnum)1, true, true};

  key k1{first}, k2{second}, k3{third};

  std::cout << (k1 == k2) << "\n"; //true
  std::cout << (k2 == k3) << "\n"; //false
  std::cout << (k3 == k1) << "\n"; //false

```

full example program: <http://cpp.sh/2vv2a>

It's obviously a wrong assumption but you can try to reduce the key size if you can make all possible value for fleetId fit in 16 bits or shorten OwnershipEnum as for example