Automatic initialization of map()?

E

Eric Lilja

Hello, I have class that acts as a lookup table. The class has two static
member variables of type std::map and two public static member functions
that allows the user to query the maps (return value for given key, if the
map contains such a key). I was wondering how I should initialize the maps,
if I could manage to perform it automatically then my file where I have
other initialization code doesn't have to include the class header just to
call init. I guess this cannot be done but I wanted to ask anyway.

/ Eric
 
P

phil_gg04

I have class that acts as a lookup table. The class has two static
member variables of type std::map and two public static member functions
that allows the user to query the maps (return value for given key, if the
map contains such a key). I was wondering how I should initialize the maps,
if I could manage to perform it automatically then my file where I have
other initialization code doesn't have to include the class header just to
call init. I guess this cannot be done but I wanted to ask anyway

Yes, it can be done. I'd do it using an initialise-on-first-use
approach:

private:
bool initialised;
void initialise(void) { ... ; initialised=true; }
public:
T get_something(x) { if (!initialised) initialise(); return ....; }


--Phil.
 
S

Shezan Baig

Eric said:
Hello, I have class that acts as a lookup table. The class has two static
member variables of type std::map and two public static member functions
that allows the user to query the maps (return value for given key, if the
map contains such a key). I was wondering how I should initialize the maps,
if I could manage to perform it automatically then my file where I have
other initialization code doesn't have to include the class header just to
call init. I guess this cannot be done but I wanted to ask anyway.

/ Eric


I would suggest using the Singleton pattern.

Hope this helps,
-shez-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,065
Members
47,672
Latest member
svaraho

Latest Threads

Top