Note that there are some explanatory texts on larger screens.

plurals
  1. POObject Factory with different parameters
    primarykey
    data
    text
    <p>I've been looking at factory method and struggled to find a solution to my problem (although i have the feeling it is straight forward.I'm trying to create objects that come from the same derived class, which is know in advance but they have different parameters. </p> <pre><code>class Base { public: Base(){}; ~Base(){}; std::string name; double base_input; double output; virtual void relation_function()=0; }; class Derived1 : public Base { public: double private_input; int multiplier; Derived1(std::string , double , double , int); ~Derived1(){}; virtual void relation_function(); }; class Derived2 : public Base { public: double private_input; int multiplier; Derived2(std::string , double , int); ~Derived2(){}; virtual void relation_function(); }; </code></pre> <p>the parameters are injected in the derived class based on their constructors. </p> <pre><code> Derived1::Derived1(std::string input_name, double input_base_input,double input_private_input, int input_multiplier){ name=input_name; base_input=input_base_input; private_input=input_private_input; multiplier=input_multiplier; }; Derived2::Derived2(std::string input_name,double input_private_input,int input_multiplier) { name=input_name; private_input=input_private_input; multiplier=input_multiplier; void relation_function();}; void Derived2:: relation_function(){output=multiplier*private_input;}; void Derived1:: relation_function(){output=multiplier*base_input*private_input;}; </code></pre> <p>Currently i'm creating instance of the derived class manually as follows</p> <pre><code>std::vector&lt;std::string&gt; v(3); v[0]="a";v[1]="b";v[2]="c"; for (int n=0;n&lt;=2;n++) Base* pderived1(new Derived1(v[n],2,2,1)); std::vector&lt;std::string&gt; v(2); v[0]="d";v[1]="e"; for (int n=0;n&lt;=1;n++) Base* pderived1(new Derived1(v[n],5,9,9)); </code></pre> <p>which is not ideal, i need to create first a pointer to the constructor of the derived class to "fix"/"freeze" some of the paramters in the constructor functions before a number of instances are created from each derived class.</p> <pre><code>base* (*pconstructor){string, double, double, int) = Derived (string, 2,2,1) </code></pre> <p>the aim is to use this pointer to the constructor as the main tool to dicate the paramaters before passing to the following functions to create the object. the function below would act as a factory to create the number of instances/objects required from derived1 or derived which may have different parameters in their constructor functions like derived2.</p> <pre><code>base* function(std::vector&lt;string&gt;){ create instances.. } </code></pre> <p>i dont know how to create the pointer to manipulate the constructor parameters nor the function that would be used to create the instances.. Any clues, please.. Thanks you all in advance for your help from a c++ novice!</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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