pa**********@att.net wrote:
If anyone could possibly illustrate and explain the concept of "factory
functions", I would be very grateful.
I tried googling and I tried the FAQ but couldn't get enlightenment.
(If you can find a URL which explains this with an example, I'd be
grateful.)
A factory is simply a "creator" of objects.
When you mix in the idea of polymorphic classes it becomes more
interesting since the factory may create more than one type of object.
e.g.
struct C { virtual int Yo() = 0; };
struct C1 : C { int Yo() { return 1; } };
struct C2 : C { int Yo() { return 2; } };
C * Cfactory( const int & itype )
{
switch ( itype )
{
case 1 : return new C1;
case 2 : return new C2;
default : throw badalloc;
}
}
This then becomes even more interesting when you want to be able to
dynamically load libraries (dlopen() or LoadLibrary()) and be able to
access classes in the library. For this you need something like the
generic factory mechanism in Austria C++ (shameless plug).
G