Telenoobies
Telenoobies

Reputation: 936

Populating a hash table from #define macros

I want to create a hash map/table by populating the key using #defined parameters and the hash value with the value corresponding the the defined parameters... So I can easily add more parameters in the future.

        //key    //value
#define paramA   1
#define paramB   2

Is there any way of doing so?

Upvotes: 1

Views: 314

Answers (1)

John Humphreys
John Humphreys

Reputation: 39294

Regardless of how you create your table, you're going to have to populate it explicitly somehow by adding these members to the table. So, it doesn't matter if your members are #defined, set up as consts, or whatever - the work is still on adding them to the table.

I'd say the best you could probably do (given your goal) is to set up your #define to actually define an array of values, and set up a second #define to define the size of that array. You could then have a loop populate your hash table by cycling through the array based on the size.

I think this sounds like a design smell though, you might want to rethink what you're doing and look for a better way - like maybe reading from a database or file?

Upvotes: 2

Related Questions