具有不同参数的对象工厂
我一直在研究工厂方法,并努力寻找解决我的问题的方法(尽管我感觉它很简单。我正在尝试创建来自同一派生类的对象,这是事先知道的,但它们有不同的参数。
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();
};
参数根据其构造函数注入到派生类中。
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;};
目前,我正在手动创建派生类的实例,
std::vector<std::string> v(3);
v[0]="a";v[1]="b";v[2]="c";
for (int n=0;n<=2;n++)
Base* pderived1(new Derived1(v[n],2,2,1));
std::vector<std::string> v(2);
v[0]="d";v[1]="e";
for (int n=0;n<=1;n++)
Base* pderived1(new Derived1(v[n],5,9,9));
这并不理想,我需要首先创建一个指向派生类的构造函数的指针。 “修复”/“冻结”一些参数在从每个派生类创建多个实例之前,在构造函数中,
base* (*pconstructor){string, double, double, int) = Derived (string, 2,2,1)
目的是使用指向构造函数的指针作为主要工具来指示参数,然后再传递给以下函数以创建下面的函数将执行的操作。作为创建衍生1或衍生所需的实例/对象数量的工厂,这些实例/对象在其构造函数(如衍生2)中可能具有不同的参数,
base* function(std::vector<string>){ create instances.. }
我不知道如何创建指针来操作构造函数参数,也不知道如何创建用于创建的函数。实例..请提供任何线索.. 预先感谢大家对 C++ 新手的帮助!
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.
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();
};
the parameters are injected in the derived class based on their constructors.
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;};
Currently i'm creating instance of the derived class manually as follows
std::vector<std::string> v(3);
v[0]="a";v[1]="b";v[2]="c";
for (int n=0;n<=2;n++)
Base* pderived1(new Derived1(v[n],2,2,1));
std::vector<std::string> v(2);
v[0]="d";v[1]="e";
for (int n=0;n<=1;n++)
Base* pderived1(new Derived1(v[n],5,9,9));
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.
base* (*pconstructor){string, double, double, int) = Derived (string, 2,2,1)
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.
base* function(std::vector<string>){ create instances.. }
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从问题来看,尚不清楚实际目标是什么。但是,我不知道是否可以有一个指向构造函数/析构函数的成员函数的指针。所以你必须放弃这个选择。
最好在构造函数实例本身时进行任何检查。另外,以下是一个坏主意,因为它会泄漏内存:
您多次覆盖
pdriven1
。谨慎使用new
/malloc
。From the question, it's unclear that what's the actual goal. However, I am not aware if you can have a pointer to member function for constructor / destructor. So you have to give up for that option.
It's better to do whatever check while constructor instance itself. Also following is a bad bad idea, as it leaks memory:
You are overwriting
pderived1
more than once. Cautious with use ofnew
/malloc
.解决这个问题的好方法就是提供具有不同参数的函数:
编辑:这种设计还需要一些可能难以弄清楚的东西,特别是以下类:
然后可以实现像 reg1 这样的函数来存储 new FactoryFunction1< ;T,P1>(fptr) 到
std::vector
。显然 reg1/reg2/reg3 函数也可以将 std::string 作为参数。
编辑:哦,reg4 只是缺少实现(您还需要实现其他功能)。
希望它现在可以编译:)
good solution to this problem is just providing functions with different parameters:
Edit: This design also requires some stuff that might be difficult to figure out, in particular the following classes:
Then a function like reg1 could be implemented to store
new FactoryFunction1<T,P1>(fptr)
to astd::vector<FactoryFunction*>
.Obviously reg1/reg2/reg3 functions can have std::string as a parameter too.
Edit: oh, reg4 is just missing implementation (you need to implement other functinons too).
Lets hope it compiles now :)