Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: using a static stl map

Reply
Thread Tools

Re: using a static stl map

 
 
White Wolf
Guest
Posts: n/a
 
      08-23-2003
Bart wrote:
> Hi all,
>
> I'm trying to figure something out for quite a while now.
> Say a class A has a stl map containing instances of class A. The
> "uniqueness" of the elements in the map is just a string. I want this
> map to be available to every instance of A so I make it static. After
> a lot of trying this leads to two questions from my side:
>
> -- are there any mistakes/newbie errors in the approach above?


Only a trap: that map will be shared amongst all instances of class A.
Should your program ever use threads, you will need to properly protect that
static map from being modified in one thread and accessed in any way from
another. There is a design decision to be made, which you will read in the
last paragraph of this post.

> -- How would you implement such a thing.


What you need (if I understood you well) is that all the constructors add
the new instance to the map. For that you will need at least one mandatory
argument for all constructors: the string, which is the key of your map.
The destructor would of course take the "record" out of the map. Meaning
that constructors would add the value of "this" to the static
map<std::string, A*> member and destructors delete it. To find out how to
use map make a Google search for "stl tutorial". Try to find one which is
fairly recent and seems to come from a good source. Google even seems to
have a category for STL tutorials at
http://directory.google.com/Top/Comp...utorials/?il=1

As it seems that your instances of class A are uniquely identified (you talk
about map and not multimap) there will be a bit of a trouble with
implementing a copy constructor. Depending on what does really your class
represent you will either not implement one (meaning you will disable it by
making its signature private) or you will implement it, but it will not
touch the key, only the "data" part. Same for the copy assigment operator.
This is IMHO an important part of the problem: to decide if these object can
be copied in any way or not. It is clear that since a map is chosen there
can be no two (or more) object sharing the same key, so the previously
mentioned decision has to be made.

--
WW aka Attila


 
Reply With Quote
 
 
 
 
Alexander Terekhov
Guest
Posts: n/a
 
      08-24-2003
"White Wolf" <> wrote in message news:<bi8md4$gp9$>...
[...]
> > -- are there any mistakes/newbie errors in the approach above?

>
> Only a trap: that map will be shared amongst all instances of class A.
> Should your program ever use threads, you will need to properly protect that
> static map from being modified in one thread and accessed in any way from
> another. There is a design decision to be made, which you will read in the
> last paragraph of this post.


Right.

>
> > -- How would you implement such a thing.

>
> What you need (if I understood you well) is ...


<last paragraph of this post>

http://groups.google.com/groups?selm...17C09%40web.de
(Subject: Re: Objects in container: creation and deletion details)

</last paragraph of this post>

regards,
alexander.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
STL map or hash map using struct as data and find it kl C++ 7 01-01-2008 11:05 AM
Problem using STL list and map objects as static member variables Bit byte C++ 15 07-24-2006 02:47 PM
Static Map initialized with Static Function but error utab C++ 5 05-15-2006 11:23 PM
a stl map which use stl pair as the key Allerdyce.John@gmail.com C++ 2 02-22-2006 07:25 AM
STL: Map of maps possible, but no multi-map of maps? Workarounds? Marcus C++ 2 12-09-2005 06:34 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57