I am trying to create a class which stores its instances in a map like so:
class Apple {
public:
static Apple* getApple(const std::string &name) {
auto it = allApples.find(name);
if (it == allApples.end()) // if this apple doesnt exist, make a new one
return new Apple(name);
else // if the apple exists, return its pointer
return it->second;
}
private:
static std::unordered_map<std::string, Apple*> allApples =
std::unordered_map<std::string, Apple*>();
Apple(const std::string &name) {
// create an apple
allApples.insert({ name, this });
}
}
Now, I made classes which store static Apples like so:
class Orange {
static Apple *myOrangeApple;
}
myOrangeApple = Apple::getApple("orange");
When I run the program, it crashes on the first line of the getApple() method with a vector subscript out of range error. I have tried looking around but I can't really find any solution which deals with maps and this out of range error. My best guess from some of the research I did is that it is something about the static order initialization, but I really am not too sure.
Use a function-scope static object.
This is one of standard ways to fight the static initialization order fiasco. It works because a block-scope static object is guaranteed to be initialised when the block is executed for the first time.
This method will not work if there are mutual dependencies between objects managed this way.