Boost logo

Boost :

From: Dean Michael Berris (mikhailberis_at_[hidden])
Date: 2007-09-01 20:56:02


On 9/1/07, Tobias Schwinger <tschwinger_at_[hidden]> wrote:
> Marco Costalba wrote:
> > This is a simple yet interesting object factory.
>
> Please see
>
> http://tinyurl.com/35vlvb
>
> . It seems with that factory utility one could achieve the semantics of
> your proposal somehow like this (note: untested code for illustration
> only - see docs and tests):
>
[snip code]
>
> It's pretty flexible, isn't it? It also works with smart pointers and
> there's a variant for by-value construction :-).
>

A similar approach could be done with
http://dispatcher.sourceforge.net/ -- only this time it's not required
that the function registered is a "factory". It could very well be any
function registered.

base_t * foo(int a, int b) { ... };

// somewhere in code
dispatcher<string, base_t*(int, int)> d;
d["T1"] = factory<T1>();
d["T2"] = factory<T2>();
d["something else"] = &foo;

string input;
cin >> input;
base_t holder = d[input](1, 2);

HTH

-- 
Dean Michael C. Berris
http://cplusplus-soup.blogspot.com/
mikhailberis AT gmail DOT com
+63 928 7291459

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk