Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Forcing static member initialisation
- class MyClass
- {
- static MapType s_myMap;
- public:
- static const MapType& getTheMap()
- {
- if (s_myMap.empty())
- {
- // Populate the map
- }
- return s_myMap;
- }
- };
- class MyClass
- {
- public:
- static const MapType& getTheMap()
- {
- static MapType s_myMap;
- if (s_myMap.empty())
- {
- // Populate the map
- }
- return s_myMap;
- }
- };
- struct MapInitData
- {
- char const* key; // Or whatever type is needed.
- char const* value; // Or whatever type is needed.
- operator MapType::value_type() const
- {
- return MapType::value_type( key, value );
- }
- };
- MapInitData const mapInitTable[] =
- {
- { "key1", "value1" },
- // ...
- };
- MapType const ourMap( begin( mapInitTable ), end( mapInitTable ) );
- SomeGlobal object = MyClass::getTheMap();
Add Comment
Please, Sign In to add comment