単一の引数を取るコンストラクターを持つテンプレート化されたクラスがあります。2番目のテンプレート型の引数を取る関数への関数ポインタ。そして、最初のテンプレートタイプの要素を返します。
コードは次のとおりです。
cache_controller.hpp:
template<class TYPE, class KEY>
class cache
{
private:
TYPE (*create_func)(KEY);
public:
cache(TYPE (*func)(KEY));
};
extern sf::Texture * create_texture(std::string path);
cache_controller.cpp:
template<class TYPE, class KEY>
cache<TYPE, KEY>::cache(TYPE (*func)(KEY))
{
this->create_func = func;
}
sf::Texture * create_texture(std::string path)
{
sf::Texture * tex_p = new sf::Texture;
tex_p->loadFromFile(path);
return tex_p;
}
テストコード:
cache<sf::Texture *, std::string> tcache(&create_texture);
ただし、リンクすると次のエラーが発生します。
[Linker error] main.cpp:11: undefined reference to `cache<sf::Texture*, std::string>::cache(sf::Texture* (*)(std::string))'
もちろん、任意のキー、値、および作成関数を使用してオブジェクトキャッシュを実装する簡単な方法があれば、私はすべて耳を傾けています。