Programming structure

I need a programming structure whereby a factory interface creates instances of a class defined in another module. For example,

dll-1
Defines the factory interface and base class

dll-2
Derives from the base actor to create a specialized actor class

exe
Should be able to create instances of specialized actor using the factory interface in dll-1 and a unique identifier(string or an integer) in a call that resembes
Baseclass* instance = Factory.NewInstance("Unique Identifier for specialized class");

How do i achieve this?
I suppose you could have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Base{ ... };

typedef Base*(*Constructor)();

struct Factory{
   std::map< std::string, Constructor > m;

   void bind( std::string str, Constructor c ){
      m[str] = c;
   }
   Base* create( std::string str ){
      return (* m[str])();
   }
};

1
2
3
4
5
6
struct Child : Base {
   ...
   static Base* create(){
      return new Child;
   }
};

1
2
3
4
5
6
int main(){
   Factory f;
   f.bind( "identifier", Child::make );
   Base* b = f.create( "identifier" );
   ...
}
Thank you hamsterman.. never thought from that point of view.
Topic archived. No new replies allowed.