Connecting Tech Pros Worldwide Help | Site Map

Automatic initialization of map()?

  #1  
Old July 23rd, 2005, 05:58 AM
Eric Lilja
Guest
 
Posts: n/a
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


  #2  
Old July 23rd, 2005, 05:58 AM
phil_gg04@treefic.com
Guest
 
Posts: n/a

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.

  #3  
Old July 23rd, 2005, 05:58 AM
Shezan Baig
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wrong static initialization order Sandro Bosio answers 1 March 15th, 2007 10:24 AM
assignment/initialization of container - map xuatla answers 8 July 18th, 2006 11:55 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM