Not sure this is going to be easy to explain but ...
I have a class (A) that needs to have access to a map (mapCodes), this map is the same for all instances of class(A), therefore I don't want to create an instance of the map for each instance of class A and populate it, etc ....
This is my code right now:
A.h (header file)
1 2 3 4 5 6 7 8
class A
{
private:
static map<int, string> mapCodes;
public:
A(); // constructor
};
Now, for every object of class A I call the constructor which uses the same STATIC mapCodes (which is good) but the constructor also repopulates it each time ... what a waste ...
Isn't there a way I can ... declare mapCodes and its initialization as static so I only have 1 instance populated once?
I was looking into doing something like using a STATIC CLASS or STATIC STRUCT but couldn't seem to get it to work - when doing some reading a lot of people were saying that c++ doesn't really support STATIC CLASSES...
You can have a nested class (with the map as member) which populates the map on its constructor, then a static object of that class in your class 'A'
or you can have a static bool used to check whether a constructor was already called and fill your map only if it wasn't