タイプを登録するファクトリークラスがあります
class Factory
{
public:
template<class T>
static void regist()
{
mMap[T::type()] = [](){return new T();};
}
static Base* create(string type)
{
... // use map to find the function to create a new object
}
private:
static map<string, function<Base* ()>> mMap;
};
map<string, function<Base* ()>> Factory::mMap;
Base の具象クラス T
class A : public Base
{
public:
static string type() { return "A"; }
static bool sRegist;
A() {}
};
bool A::sRegist = []() -> bool {
Factory::regist<A>();
return true;
}();
ただし、コードを実行するとクラッシュします。静的メンバーの初期化順序が不定であることが原因だと思います。それを機能させる方法は?ありがとう。