I know * defines a pointer… does ** define a pointer to a pointer?
If so, why?
Is a pointer to a pointer some times known as a reference? Just need clarification for the following very simple hash.
Generally pointers are used to pass the location of larger structures when passng the entire contents would be too costly.
I’ve seen pointers to pointers used in the quantlib project to create “handles” as each “observer” holds a pointer to a “term structure” pointer that might change at run-time, hence the pointer held the location of another pointer.
However I see no correlation here?
class hash_entry
{
private:
int key;
int value;
public:
hash_entry(int key, int value)
{
this->key = key;
this->value = value;
}
int getKey()
{
return key;
}
int getValue()
{
return value;
}
};
class hash_map
{
private:
hash_entry **table;
static const int TABLE_SIZE = 128;
public:
hash_map()
{
table = new hash_entry*[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = NULL;
}
int get(int key)
{
int hash = (key % TABLE_SIZE);
while (table[hash] != NULL && table[hash]->getKey() != key)
hash = (hash + 1) % TABLE_SIZE;
if (table[hash] == NULL)
return -1;
else
return table[hash]->getValue();
}
void put(int key, int value)
{
int hash = (key % TABLE_SIZE);
while (table[hash] != NULL && table[hash]->getKey() != key)
hash = (hash + 1) % TABLE_SIZE;
if (table[hash] != NULL)
delete table[hash];
table[hash] = new hash_entry(key, value);
}
~hash_map()
{
for (int i = 0; i < TABLE_SIZE; i++)
if (table[i] != NULL)
delete table[i];
delete[] table;
}
};
Yes,
**defines a pointer to a pointer (because the spec says so). No, I can’t quite imagine anybody calling that a reference.As to why they’re using it in this case, they’re writing (very C-like) code to dynamically allocate an array of pointers to X. This code:
Is roughly equivalent to:
(though for the moment, I haven’t split it up as you’d need to for a class member).