0

I want to call readFromMap without instantiating the HelperClass.

So I wanted to check if I can call readIntoMapFromDataStore at (some think like) class-load?

So that map is always initialized before calling readFromMap?

Initialize static map

class HelperClass {

static map<string, string> cache;

static string readFromMap(string key) {
return cache.at(key);
}

static void readIntoMapFromDataStore() {
//read contents into cache from datastore
}
}

How to initialize before calling readFromMap?

t.niese
  • 39,256
  • 9
  • 74
  • 101
user2116990
  • 51
  • 1
  • 6
  • Your code is missing some important things. The semicolon after the class. The definition of the static member (that will determine which constructor is used, the code shown will not invoke a default constructor). – Ben Voigt May 19 '19 at 06:05

1 Answers1

0

The population of the map should be done by a function that returns a map object, NRVO will eliminate the temporaries. You connect it via using initialization in the definition of the static member:

std::map<std::string, std::string> HelperClass::cache{HelperClass::readIntoMapFromDataStore()};
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720