Connecting Tech Pros Worldwide Forums | Help | Site Map

Automatic initialization of map()?

Eric Lilja
Guest
 
Posts: n/a
#1: Jul 23 '05
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



phil_gg04@treefic.com
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Automatic initialization of map()?


> I have class that acts as a lookup table. The class has two static[color=blue]
> 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[/color]

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.

Shezan Baig
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Automatic initialization of map()?




Eric Lilja wrote:[color=blue]
> 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[/color]


I would suggest using the Singleton pattern.

Hope this helps,
-shez-

Closed Thread