factory functions in c++

P

pauldepstein

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.)

Thank you very much for your help

Paul Epstein
 
G

Gianni Mariani

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
 
A

Aurelien RAINONE

Le Sun, 17 Sep 2006 14:16:01 -0700, pauldepstein a écrit :
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.)

Thank you very much for your help

Paul Epstein

If you want to look at a complete and working implementation of the
factory concept, look at this topic on this newsgroup :

Object Factory Design Pattern by GoF, need help!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top