I have a C++ code looks like this
Parent.hpp
class Parent {
static std::map<std::string, std::function<void()>> list;
}
Parent.cpp
#include "Parent.hpp"
std::map<std::string, std::function<void()>> Parent::list;
Child.hpp
#include "Parent.hpp"
class Child : Parent {
static bool isRegistered = registerComponent();
std::function<void(GameObject* go, void* arr)> add;
static bool registerComponent();
Child.cpp
#include "Child.hpp"
static bool Child::isRegistered = registerComponent();
std::function<void(GameObject* go, void* arr)> Child::add = []() {
//do something
}
static bool Child::registerComponent() {
if (add) {
list["child"] = Child::add;
return true;
}
else return false
}
is list
guaranteed to be initialized before I call registerComponent()
? I have read this post When are static C++ class members initialized? I think it's not guaranteed, but I am not 100% sure.