Connecting Tech Pros Worldwide Help | Site Map

Automatic initialization of map()?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 04:58 AM
Eric Lilja
Guest
 
Posts: n/a
Default Automatic initialization of map()?

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, 04:58 AM
phil_gg04@treefic.com
Guest
 
Posts: n/a
Default 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, 04:58 AM
Shezan Baig
Guest
 
Posts: n/a
Default 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-

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.