Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>good solution to this problem is just providing functions with different parameters:</p> <pre><code>#include &lt;string&gt; #include &lt;typeinfo&gt; #include &lt;vector&gt; class FactoryFunction; class Factory { public: template&lt;class T, class P1, class P2&gt; void reg2(T (*fptr)(P1, P2)); template&lt;class T, class P1, class P2, class P3&gt; void reg3(T (*fptr)(P1,P2,P3)); template&lt;class T, class P1, class P2, class P3, class P4&gt; void reg4(T (*fptr)(P1,P2,P3,P4)); private: std::vector&lt;FactoryFunction*&gt; vec; }; Base *derived1_factory(std::string s, double d1, double d2, int i) { return new Derived1(s,d1,d2,i); } int main() { Factory f; f.reg4(&amp;derived1_factory); } </code></pre> <p>Edit: This design also requires some stuff that might be difficult to figure out, in particular the following classes:</p> <pre><code>class FactoryFunction { public: virtual int NumParams() const=0; virtual void set_parameter(int i, void *p)=0; virtual std::string parameter_type(int i) const=0; virtual void *return_value() const=0; virtual std::string return_type() const=0; }; template&lt;class T, class P1&gt; class FactoryFunction1 : public FactoryFunction { public: FactoryFunction1(T (*fptr)(P1)) : fptr(fptr) { } int NumParams() const { return 1; } void set_parameter(int i, void *p) { switch(i) { case 0: param1 =*(P1*)p; break; }; } std::string parameter_type(int i) const { switch(i) { case 0: return typeid(P1).name(); }; } void *return_value(int i) const { return_val = fptr(param1); return (void*)&amp;return_val; } std::string return_type() const { return typeid(T).name(); } private: T (*fptr)(P1); T return_val; P1 param1; }; </code></pre> <p>Then a function like reg1 could be implemented to store <code>new FactoryFunction1&lt;T,P1&gt;(fptr)</code> to a <code>std::vector&lt;FactoryFunction*&gt;</code>. Obviously reg1/reg2/reg3 functions can have std::string as a parameter too.</p> <p>Edit: oh, reg4 is just missing implementation (you need to implement other functinons too).</p> <pre><code> template&lt;class T, class P1, class P2, class P3, class P4&gt; void Factory::reg4(T (*fptr)(P1,P2,P3,P4)) { vec.push_back(new FactoryFunction4(fptr)); } </code></pre> <p>Lets hope it compiles now :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload