By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,885 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

factory functions in c++

P: n/a
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

Sep 17 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
<pa**********@att.netwrote:
>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.)
Try this wikipedia page:

http://en.wikipedia.org/wiki/Factory_method

Steve
Sep 17 '06 #2

P: n/a
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

Sep 17 '06 #3

P: n/a
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!!
Oct 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.