Guest User

Untitled

a guest
Aug 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. Forcing static member initialisation
  2. class MyClass
  3. {
  4. static MapType s_myMap;
  5. public:
  6. static const MapType& getTheMap()
  7. {
  8. if (s_myMap.empty())
  9. {
  10. // Populate the map
  11. }
  12. return s_myMap;
  13. }
  14. };
  15.  
  16. class MyClass
  17. {
  18. public:
  19. static const MapType& getTheMap()
  20. {
  21. static MapType s_myMap;
  22. if (s_myMap.empty())
  23. {
  24. // Populate the map
  25. }
  26. return s_myMap;
  27. }
  28. };
  29.  
  30. struct MapInitData
  31. {
  32. char const* key; // Or whatever type is needed.
  33. char const* value; // Or whatever type is needed.
  34. operator MapType::value_type() const
  35. {
  36. return MapType::value_type( key, value );
  37. }
  38. };
  39.  
  40. MapInitData const mapInitTable[] =
  41. {
  42. { "key1", "value1" },
  43. // ...
  44. };
  45.  
  46. MapType const ourMap( begin( mapInitTable ), end( mapInitTable ) );
  47.  
  48. SomeGlobal object = MyClass::getTheMap();
Add Comment
Please, Sign In to add comment