class AudiTTFactory : public KarossFactory
{
private:
static const AudiTTFactory registerThis;
AudiTTFactory() : KarossFactory("AudiTT") {};
Kaross* makeObject()const
{
return new AudiTT();
}
};
class KarossFactory : public Factory<Kaross>
{
public:
KarossFactory(const string &className) : Factory<Kaross>(className) {};
};
template
<class Object> class Factory
{
public:
typedef Factory<Object>* FactoryPtr;
typedef map<string, FactoryPtr> FactoryMap;
static FactoryMap registry;
virtual ~Factory(){};
Factory(const string& className)
{
registry->insert( std::make_pair(className, this) );
};
static Object* newObject(const string& className)
{
Factory<Object> *maker = Factory<Object>::registry.find(className)->second;
return maker->makeObject();
};
virtual Object* makeObject() const=0;
};
Register Factories - static map in templated class
I have taken a look at the article Industrial Strength Pluggable Factory found in C++ Report from 99, in the end the author describes how they made a templated version of the Maker class. Im having problems trying to implement this, the problem is with the static map containing references instances of each factory along with a name. I have tried a number of different ways in the static newObject method to access the static map and retrieve the reference to the named factory instance but with no luck.
With this code I get a LNK2020 error:
error LNK2020: unresolved token (0A000075) ?registry@?$Factory@VKaross@@@@2V?$map@V.....
PluggableFactory error LNK2020: unresolved token (0A000075) ?registry@?$Factory@VKaross@@@@2V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAV?$Factory@VKaross@@@@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAV?$Factory@VKaross@@@@@std@@@2@@std@@A
Looks like you need to add:
template < typename Object >
typename Factory< Object >::FactoryMap Factory< Object >::registry;
You've declared your registry object, but you haven't actually defined it.
Enigma
template < typename Object >
typename Factory< Object >::FactoryMap Factory< Object >::registry;
You've declared your registry object, but you haven't actually defined it.
Enigma
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement