Index:
[thread]
[date]
[subject]
[author]
From: Jason Grosland <jcg3@cs.duke.edu>
To :
Date: Mon, 8 Feb 1999 22:52:14 -0500
Re: ReaderFactory
On Mon, 8 Feb 1999, Luke Palmer wrote:
> Okay, I sorta understand ReaderFactory, but what's the deal with the
> constructor being private? How can that work? Theoretically, if the
> constructor is private, then nobody can make an object of type
> ReaderFactory. what good is a class if you can't use it? i don't get it...
ahhh... there in lies the rub, eh?
well, what you can do is write a static function that returns an instance
of the factory. a static function is one that you can call without having
an instance of the class-- i.e., your code might look like:
// in the .h file:
static Factory * theFactory;
// in the .cc file:
static Factory * makeFactory() {
if(theFactory != NULL) {
theFactory = new Factory();
}
return theFactory;
}
// in usage:
Factory * foo;
foo = Factory.makeFactory();
FYI-- I can't remember if the convention for private static variables
might be 'ourFactory' rather than 'theFactory'-- buyer beware.
Do think about what kind of issues this might bring up... Who is going to
free the memory for the Factory? How might you keep track of whether or
not it can be safely deleted?
The above is an example of the singleton pattern in use... Also think
about whether or not you might want to use inheritance with a Factory
inheritance tree? How would a static function like this behave in
inheritance (I believe the answer is in Effective C++, though I'm not
sure)...
-Jason
cps 108 uta
- jcg3@duke.edu shanty.dorm.duke.edu www.duke.edu/~jcg3 -
Index:
[thread]
[date]
[subject]
[author]