What is the problem with the following call to getHashTable?

QUESTION

What is the problem with the following call to getHashTable?

HashTable *getHashTable()

{

    HashTable<<LLData>table

    return &table;

}

//Here is the call to getHashTable

HashTable *p_table = getHashTable();

LLData dataTest;

p_table->insert(dataTest);

Select one:

a) The hash table was returned without any elements in it.

b) You must always call new on a hash table. You cant make a static object

c) You cant return the pointer to an object

d) p_table points to a HashTable that no longer exists(a table is a static object)

 

ANSWER

The correct answer is option-d.

P_table points to a HashTable that no longer exists (a table is a static object).

 

EXPLANATION

The getHasTable function is defined and it returns a pointer to the HashTable template which takes in LLData type. 

After this, we call to getHashTable function and it is assigned to the p_table variable of the HashTable type. The LLData object is then declared and is tried to be inserted in the p_table. 

The problem in this is that the HashTable type is static inside the function getHashTable. This means that after calling to getHashTable function, it would no longer exist and therefore it cannot be called or assigned. 

 

Also read: Why are iterators useful?

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *