473,473 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

factory functions in c++

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
4 9357
<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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Marco Aschwanden | last post by:
Hi I don't have to talk about the beauty of Python and its clear and readable syntax... but there are a few things that striked me while learning Python. I have collected those thoughts. I am...
17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
6
by: Boogie El Aceitoso | last post by:
Hi, I'd like to have a function factory that returns objects of a class hierarchy. What's the best way to deal with the fact that different subclasses will have different constructor arguments?...
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
10
by: Chris Croughton | last post by:
What do people call their factory functions? 'new' is not an option (yes, it can be overloaded but has to return void*). The context is: class MyClass { public: // Factory functions...
6
by: Dave | last post by:
Hello all, Please see my question embedded in comment form below. Thanks, Dave #include <iostream> #include <boost/shared_ptr.hpp>
4
by: anonymous.user0 | last post by:
Using the dotnet v1.1 framework (so no generics possible). I'd like to create a bunch of Factory classes that all inherit from a single abstract base Factory class. Each Factory is responsible...
5
by: ma740988 | last post by:
Consider: #include "handyfactory.h" #include <iostream> struct Shape { virtual void print() const=0; };
7
by: Steven T. Hatton | last post by:
I have a couple questions about the design pattern presented in the example quoted below. I can appreciate why the destructor is protected, but why is it not virtual? I am forced to assume that I...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.