Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Initialising STL Container which is Static member

Status
Not open for further replies.

antmd

Programmer
Feb 8, 2002
3
GB
Hi
I'm a newbie to C++
I've been trying to find out the best way of initialising a static (private) data member of a class, which is an STL container (Multimap).
I know one can initialise 'simple' static member data types in the definition, by just applying the constructor. e.g. int myClass::myInt(4).
However, when it comes to initialising a static data member which is an STL container, I couldn't think of an easy way.
My solution was to inherit from the STL Container class, defining my own class, and have the constructor for my STL-derived class do it's initialisation (using 'insert' to add some constant mappings).
I am wary of ineriting from the STL, so I have two questions:

1) Is inheriting from the STL classes good, bad or neither?
2) Is there a better way of initialising complex static data members?

I suppose I could have a separate 'initialise' method, but it seems a bit inelegant, given that the class should be ready to use as soon as it's defined.

Best Regards
--Anthony
 
Can't you create a init method and then call that method in your constructor ? The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
Hi Muppeteer,
I forgot to mention that the multimap data is kinda like a constant... I would prefer not to re-initialise every time I created a new instance of the class. Or is the compiler smart enough to know not to re-initialise static data? Isn't the contructor called every time a new instance is created?

Regards
--Anthony
 
Yes, but you could make a static var (sort of a switch,static int's are automatically initialised on 0) that you put on 1 the first time you do the init method, and before the init method is called,you check wether your switch var is 0 or 1. That way,the method is called only once :) The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top