View previous topic :: View next topic |
Author |
Message |
not_registered Tux's lil' helper
Joined: 04 Feb 2003 Posts: 148
|
Posted: Sat Jun 07, 2003 3:08 am Post subject: c++ factory design pattern with templates? |
|
|
is it possible?
currently the concrete child class isnt recognised so something something. or is there a better way to export something to any kind of STREAM given a STREAM? _________________ It's Floam, it's Floam. It's flying foam! |
|
Back to top |
|
|
Braempje l33t
Joined: 31 Jan 2003 Posts: 748
|
|
Back to top |
|
|
fragbert Tux's lil' helper
Joined: 18 Apr 2003 Posts: 75 Location: Dallas, TX
|
Posted: Sat Jun 07, 2003 8:26 am Post subject: |
|
|
I'm not sure what you're asking, but that hasn't stopped me from answering in the past, so here we go...
Yes, you can use a templated factory to avoid massive subclassing. This is covered in Design Patterns under "Factory Method" in Ch 3. Here is the code snippet they provide:
Code: | class Creator {
public:
virtual Product * CreateProduct() = 0;
};
template <class TheProduct>
class StandardCreator : public Creator {
public:
virtual Product * CreateProduct();
};
template <class TheProduct>
Product * StandardCreator<TheProduct>::CreateProduct()
{
return new TheProduct;
}
|
This allows you to do:
Code: | class MyProduct : public Product() { ... };
StandardCreator<MyProduct> myCreator; |
The book can explain the rest better than I. You should pick it up if you're getting serious about OO, it is an invaluable resource.
Michael |
|
Back to top |
|
|
() l33t
Joined: 25 Nov 2002 Posts: 610
|
Posted: Sat Jun 07, 2003 9:35 am Post subject: |
|
|
Modern C++ Design and its accompanying Loki library should be exactly what you're looking for, Andrej Alexandrescu implements several design patterns with some innovative use of templates. Among other things you can find Factory and AbstractFactory (http://sourceforge.net/projects/loki-lib/).
Last edited by () on Sat Jun 07, 2003 12:44 pm; edited 1 time in total |
|
Back to top |
|
|
not_registered Tux's lil' helper
Joined: 04 Feb 2003 Posts: 148
|
Posted: Sat Jun 07, 2003 12:00 pm Post subject: |
|
|
nevermind (but thanks for your replies), i fixed my problem by puting the concrete class 'header stuff' and parent 'header stuff' in one file. gcc didnt seem to like them seperate.
i would paste the code here to show you what the hello i was doing but its for an assignment and code leaks leads to the dark side.
basically its a export thingy that exports a certain file format to a stream.
so now it works. meow. _________________ It's Floam, it's Floam. It's flying foam! |
|
Back to top |
|
|
|