Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
c++ factory design pattern with templates?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
not_registered
Tux's lil' helper
Tux's lil' helper


Joined: 04 Feb 2003
Posts: 148

PostPosted: Sat Jun 07, 2003 3:08 am    Post subject: c++ factory design pattern with templates? Reply with quote

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
View user's profile Send private message
Braempje
l33t
l33t


Joined: 31 Jan 2003
Posts: 748

PostPosted: Sat Jun 07, 2003 6:45 am    Post subject: Reply with quote

Could you be more specific as to what you're trying to do? I have used a couple of template-related factory ideas, which worked pretty well, but for the moment I don't understand what you need.
_________________
Dictionary of the Flemish Sign Language - Woordenboek Vlaamse Gebarentaal
Back to top
View user's profile Send private message
fragbert
Tux's lil' helper
Tux's lil' helper


Joined: 18 Apr 2003
Posts: 75
Location: Dallas, TX

PostPosted: Sat Jun 07, 2003 8:26 am    Post subject: Reply with quote

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
View user's profile Send private message
()
l33t
l33t


Joined: 25 Nov 2002
Posts: 610

PostPosted: Sat Jun 07, 2003 9:35 am    Post subject: Reply with quote

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
View user's profile Send private message
not_registered
Tux's lil' helper
Tux's lil' helper


Joined: 04 Feb 2003
Posts: 148

PostPosted: Sat Jun 07, 2003 12:00 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum