C++ 飞锤模式和C++;模板
我有飞锤图案。我有抽象类标志符号。我有从Glyph派生的类字母和抽象代码。我有YusciiCode、UniCyrCode和UniLatCode,它们都是从代码中派生出来的 我的flyweight工厂可以这样做:C++ 飞锤模式和C++;模板,c++,templates,flyweight-pattern,C++,Templates,Flyweight Pattern,我有飞锤图案。我有抽象类标志符号。我有从Glyph派生的类字母和抽象代码。我有YusciiCode、UniCyrCode和UniLatCode,它们都是从代码中派生出来的 我的flyweight工厂可以这样做: template <class T> class CodeFactory : public AbstractCodeFactory { public: CodeFactory(); virtual ~CodeFactory(); virtual Gl
template <class T>
class CodeFactory : public AbstractCodeFactory
{
public:
CodeFactory();
virtual ~CodeFactory();
virtual Glyph* GetFlyweight(unsigned int code);
virtual Glyph* GetFlyweight(string letter);
private:
// pool of flyweights (codes or letters)
map <unsigned int, Glyph*> my_code_map;
map <string, Glyph*> my_letter_map;
};
template <class key, class T>
class CodeFactory : public AbstractCodeFactory
{
public:
CodeFactory();
virtual ~CodeFactory();
virtual Glyph* GetFlyweight(key code);
private:
// pool of flyweights (codes or letters)
map <key, Glyph*> my_code_map;
};
模板
类CodeFactory:PublicAbstractCodeFactory
{
公众:
代码工厂();
虚拟代码工厂();
虚拟图示符*GetFlyweight(无符号整数代码);
虚拟图示符*GetFlyweight(字符串字母);
私人:
//飞锤池(代码或字母)
映射我的代码映射;
映射我的字母映射;
};
可以这样做:
template <class T>
class CodeFactory : public AbstractCodeFactory
{
public:
CodeFactory();
virtual ~CodeFactory();
virtual Glyph* GetFlyweight(unsigned int code);
virtual Glyph* GetFlyweight(string letter);
private:
// pool of flyweights (codes or letters)
map <unsigned int, Glyph*> my_code_map;
map <string, Glyph*> my_letter_map;
};
template <class key, class T>
class CodeFactory : public AbstractCodeFactory
{
public:
CodeFactory();
virtual ~CodeFactory();
virtual Glyph* GetFlyweight(key code);
private:
// pool of flyweights (codes or letters)
map <key, Glyph*> my_code_map;
};
模板
类CodeFactory:PublicAbstractCodeFactory
{
公众:
代码工厂();
虚拟代码工厂();
虚拟图示符*GetFlyweight(键代码);
私人:
//飞锤池(代码或字母)
映射我的代码映射;
};
在第一个示例中,GCC链接器告诉我没有字母(unsigned int)和xxxCode(string)构造函数。事实上没有任何构造函数,GCC是正确的,但是有没有比定义这些构造函数更好的方法呢
在Second ecample中,GCC编译器告诉我该行有错误
map <key, Glyph*>::iterator it;
map::迭代器;
函数GetFlyweight的
实现这种flyweight模式的方法是什么
我需要用它。
以下是我当前的实现:
class AbstractCodeFactory
{
public:
AbstractCodeFactory();
virtual ~AbstractCodeFactory();
virtual Glyph* GetFlyweight(unsigned int code) = 0;
virtual Glyph* GetFlyweight(string letter) = 0;
};
template <class T>
class CodeFactory : public AbstractCodeFactory
{
public:
CodeFactory();
virtual ~CodeFactory();
virtual Glyph* GetFlyweight(unsigned int code);
virtual Glyph* GetFlyweight(string letter);
private:
// pool of flyweights (codes or letters)
map <unsigned int, Glyph*> my_code_map;
map <string, Glyph*> my_letter_map;
};
template <class T>
CodeFactory<T>::CodeFactory()
{
// TODO Auto-generated constructor stub
}
template <class T>
CodeFactory<T>::~CodeFactory()
{
// TODO Auto-generated destructor stub
map <unsigned int, Glyph*>::iterator it;
map <string, Glyph*>::iterator l_it;
for (it = my_code_map.begin(); it != my_code_map.end(); ++it)
{
delete it->second;
it->second = NULL;
my_code_map.erase(it);
}
for (l_it = my_letter_map.begin(); l_it != my_letter_map.end(); ++l_it)
{
delete l_it->second;
l_it->second = NULL;
my_letter_map.erase(l_it);
}
}
template <class T>
Glyph* CodeFactory<T>::GetFlyweight(unsigned int code)
{
map <unsigned int, Glyph*>::iterator it;
T *code_class = NULL;
if ((it = my_code_map.find(code)) == my_code_map.end())
{
my_code_map.insert(pair <unsigned int, Glyph*> (code, code_class = new T(code)));
return code_class;
}
else return it->second;
}
template <class T>
Glyph* CodeFactory<T>::GetFlyweight(string letter)
{
map <string, Glyph*>::iterator it;
T *letter_class = NULL;
if ((it = my_letter_map.find(letter)) == my_letter_map.end())
{
my_letter_map.insert(pair <string, Glyph*> (letter, letter_class = new T(letter)));
return letter_class;
}
else return it->second;
}
类AbstractCodeFactory
{
公众:
抽象代码工厂();
虚拟~AbstractCodeFactory();
虚拟图示符*GetFlyweight(无符号整数代码)=0;
虚拟图示符*GetFlyweight(字符串字母)=0;
};
模板
类CodeFactory:PublicAbstractCodeFactory
{
公众:
代码工厂();
虚拟代码工厂();
虚拟图示符*GetFlyweight(无符号整数代码);
虚拟图示符*GetFlyweight(字符串字母);
私人:
//飞锤池(代码或字母)
映射我的代码映射;
映射我的字母映射;
};
模板
CodeFactory::CodeFactory()
{
//TODO自动生成的构造函数存根
}
模板
CodeFactory::~CodeFactory()
{
//TODO自动生成的析构函数存根
对它进行迭代器;
迭代器l_it;
for(it=my_-code_-map.begin();it!=my_-code_-map.end();++it)
{
删除它->秒;
it->second=NULL;
我的代码映射。删除(它);
}
for(l_it=我的字母映射。begin();l_it!=我的字母映射。end();+l_it)
{
删除l_it->second;
l_it->second=NULL;
我的字母和地图。抹掉它;
}
}
模板
Glyph*CodeFactory::GetFlyweight(无符号整数代码)
{
对它进行迭代器;
T*code_class=NULL;
if((it=my_-code\u-map.find(code))==my_-code\u-map.end())
{
my_code_map.insert(成对(代码,代码类=新T(代码));
返回代码_类;
}
否则返回->秒;
}
模板
Glyph*CodeFactory::GetFlyweight(字符串字母)
{
对它进行迭代器;
T*字母_class=NULL;
if((it=my_letter_map.find(letter))==my_letter_map.end())
{
我的字母映射。插入(成对(字母,字母类=新T(字母));
回信(类别);;
}
否则返回->秒;
}
由于您的flyweight factory只能生成字母、YusciiCode、UniCyrCode或UniLatCode对象,因此我选择第二个选项(第二个模板参数,指示键类型)
编译器在声明map::iterator it;
时遇到的问题是,编译器无法确定map::iterator
是否引用了类型或其他内容。
这是因为它取决于模板参数键
,并且在成员迭代器
不是类型的地方,您可能有一个映射
的专门化。要帮助编译器,必须指定
map::iterator
引用类型名:
typename map<key, Glyph*>::iterator it;
typename映射::迭代器;
你真的需要使用它吗,还是你只是想教育自己?如果是前者,请看一下Boost.Flyweight.We需要实现来帮助你。“[…]GCC编译器告诉我,第[…]行有错误”-人们什么时候才能学会只发布准确的错误消息?我对代码做了相当多的修改(一个功能性的)并且没有保存错误消息。谢谢,我将按照您的建议尝试实现它。