Idiom for object construction and factories in C++0X
What is the preferred naming idiom for factories, make functions, etc.
in C++ these days? Should the constructor free function be called
"make" in front of the class name, and the factory implement operator
() to construct?
For example:
template<class F>
class simplex
{
F f_;
optimizer_settings opts_;
template<class F>
simplex(F f, optimizer_settings opts) : f_(f), opts_(opts){}
};
template<class F>
simplex<F> make_simplex(F f, optimizer_settings opts)
{
return simplex<F>(f, opts);
}
class simplex_factory
{
optimizer_settings opts_;
simplex_factory(optimizer_settings opts) : opts_(opts) {}
template<class F>
simplex<F> operator()(F f)
{
return simplex<F>(f, opts_);
}
};
----------------------- Usage:
double f(double x) {return x*x;}
optimizer_settings my_opts(.1, .2); //some construction
//Can create with "make" function
auto my_simplex = make_simplex(f, my_opts);
//Or can create a factory and then generate them.
simplex_factory my_factory(my_opts);
auto my_simplex2 = my_factory(f);
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]