The value of the member in the class initialized when the object was created?

I am writing a hash class:

struct hashmap { void insert(const char* key, const char* value); char* search(const char* key); private: unsigned int hash(const char* s); hashnode* table_[SIZE]; // <-- }; 

Since insert () needs to check if table [i] is empty when a new pair is inserted, so I need all the pointers in the table to be set to NULL at startup.

My question is: will this pointer array table_ automatically initialized to zero or should I manually use a loop to set the array to zero in the constructor?

0
source share
2 answers

The table_ array will be uninitialized in your current design, as if you said int n; . However, you can initialize the array (and therefore initialize the zero of each member) in the constructor:

 struct hash_map { hash_map() : table_() { } // ... }; 
+5
source

You must set all pointers to NULL. You do not need to use a loop, you can call the constructor:

 memset(table_, 0, SIZE*sizeof(hashnode*)); 
0
source

Source: https://habr.com/ru/post/975939/


All Articles