As i tried hundreds different implementation to make it work and actually didn't succeed, can someone here help me to understand and use the implementation of the Object Factory design pattern. I'm using gcc 3.4.2 on Mingw.
The factory is the same as the one in Modern C++ design (without error checking) ,
like this :
template
<
class AbstractProduct ,
typename IdentifierType,
typename ProductCreator = AbstractProduct * (*)()
>class Factory
// : public FactoryErrorPol icy<IdentifierT ype, AbstractProduct >
{
public:
bool Register(const IdentifierType& id, ProductCreator creator)
{
return associations_.i nsert(AssocMap: :value_type(id, creator)).secon d;
}
bool Unregister(cons t IdentifierType& id)
{
return associations_.e rase(id) == 1;
}
AbstractProduct * CreateObject(co nst IdentifierType& id)
{
typename AssocMap::const _iterator i = associations_.f ind(id);
if (i != associations_.e nd())
{
return (i->second)();
}
//handle error
}
private:
typedef std::map<Identi fierType, AbstractProduct AssocMap;
AssocMap associations_;
};
and for the example i want to compile i have a simple class hierarchy like that:
class Shape
{
public:
virtual void Draw() const;
virtual void Rotate(double angle);
virtual void Zoom(double zoomFactor);
Shape* operator()() {return new Shape;} // i thought this was necessary but...
};
class Line: public Shape
{
public:
void Draw () const {return;}
void Rotate (double angle) {return;}
void Zoom (double zoomFactor) {return;}
};
class Triangle: public Shape
{
public:
void Draw () const {return;}
void Rotate (double angle) {return;}
void Zoom (double zoomFactor) {return;}
};
So what i'm doing is :
const int LINE = 1;
const int TRIANGLE = 2;
typedef Factory<Shape*, intShapeFactory ;
Line* CreateLine()
{
return new Line;
}
Triangle* CreateTriangle( )
{
return new Triangle;
}
void TEST_FUNCTION ()
{
ShapeFactory myFactory;
myFactory.Regis ter(LINE, &CreateLine) ;
myFactory.Regis ter(TRIANGLE, &CreateTriangle );
}
Compiler listing is :
src\core\object factory.cpp:24: instantiated from here
src\include\obj ectfactory.h:12 5: error: dependent-name ` std::map<Identi fierType,Abstra ctProduct,std:: less<Identifier Type>,std::allo cator<std::pair <const IdentifierType, AbstractProduct >::value_type ' is parsed as a non-type, but instantiation yields a type
src\include\obj ectfactory.h:12 5: note: say `typename std::map<Identi fierType,Abstra ctProduct,std:: less<Identifier Type>,std::allo cator<std::pair <const IdentifierType, AbstractProduct >::value_type ' if a type is meant
--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-