473,322 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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
Jul 23 '05 #1
2 1594
> 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.

Jul 23 '05 #2


Eric Lilja wrote:
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-

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Nolan Martin | last post by:
How would I go about calling a function before the main function is called? The function needs to be called multiple times from different locations. I have something like this working but it is...
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
4
by: marvind | last post by:
I think I am running into the static initialization problem but I do not understand why. I am trying to parse a configuration file. To make this parser generic I register callbacks for various...
8
by: xuatla | last post by:
Hi, I want to define a map: std::map<string, intmyMap; e.g., the score of students. Then I can assign the value as follows: myMap = 90; myMap = 60; ....
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
2
by: nerdrakesh | last post by:
Hi - I am using the map stl and is there a way I could specify the hash table size in the map in the beginning - (similar to vector.reserve(size) ). Is there a way I could specify the entries at...
3
by: Markus Dehmann | last post by:
I think this is a question about automatic type conversion, but I didn't find the answer after googling for these words ... I have a class called Value (source see below) which can hold an int...
2
by: =?Utf-8?B?RnJlZW1hbg==?= | last post by:
Hi, I'm new to vb.net. How to prevent vb.net to automatic execute code in design time? When I open a form design view in VS2005, vs pop up an error message: "Could not load file or assembly...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.