I have this class:
class LevelParser
{
private:
int tileSize;
std::vector<Tileset>* pTilesets;
typedef LayerABC* (*pFunc)();
std::unordered_map<std::string, pFunc> layerFactory;
template <typename T>
void registerLayer(std::string layerName);
LayerABC* createLayer(std::string layerName);
};
template <typename T>
void LevelParser::registerLayer(std::string layerName)
{
auto Iterator = layerFactory.find(layerName);
if(Iterator != layerFactory.cend())
{
return;
}
layerFactory[layerName] = [&] () -> LayerABC* {return new T(tileSize, pTilesets); };
}
It compiles okay, but when it comes to creating a layer via factory, the program brakes and IDE points me to the line with lambda of the 'registerLayer' function.
The LevelParser class variables 'tileSize' and 'pTilesets' get initialised later during the parsing process and before the layer creation happens, that is why they are captured by reference, and that is why lambda should work, but it does not.
Does anyone have idea what is wrong?
P.S.
There is a solution to avoid passing parameters to the layer constructor like this:
layerFactory[layerName] = [] () -> LayerABC* {return new T(); };
and initialise the object after construction via an 'init(int, std::vector<Tileset>*)' function that will actually duplicate the initialisation job that is done by the constructors - but this is the second resort.
Aucun commentaire:
Enregistrer un commentaire