gpt4 book ai didi

c++ - 自动决定使用哪个类进行数据处理

转载 作者:行者123 更新时间:2023-11-28 07:58:51 26 4
gpt4 key购买 nike

我有一个大项目,我遇到了一个问题,可以简述如下:

我有一个临时创建的类,用于处理和修改一些数据(我们称它为“worker”)。现在我有两个 worker 和两种相应的数据格式。数据数组可以包含混合数据,如何让我的程序自动决定它应该创建哪个工作类并用于数据处理?如何以最佳方式做到这一点?

为了说明这个问题,我编写了与我的项目类似的小示例程序。

#include <iostream>
#include <vector>
using namespace std;

const int NInputs = 10;



struct TOutput {
int i;
};

class TProcess {
public:
TProcess( const vector<TInput>& i ){ fInput = i; }

void Run();

void GetOutput( TOutput& o ) { o = fOutput; }
private:
vector<TInput> fInput;
TOutput fOutput;
};

#if 0
struct TInput {
int i;
};
class TWorker{
public:
void Init( int i ) { fResult = i; }
void Add( int i ) { fResult += i; }
int Result() { return fResult; }
private:
int fResult;
};
#else
struct TInput {
int i;
};
class TWorker {
public:
void Init( int i ) { fResult = i; }
void Add( int i ) { fResult ^= i; }
int Result() { return fResult; }
private:
int fResult;
};
#endif

void TProcess::Run() {
TWorker worker;
worker.Init(0);
for( int i = 0; i < fInput.size(); ++i )
worker.Add(fInput[i].i);

fOutput.i = worker.Result();
}

int main() {
vector<TInput> input(NInputs);

for ( int i = 0; i < NInputs; i++ ) {
input[i].i = i;
}

TProcess proc(input);
proc.Run();

TOutput output;
proc.GetOutput(output);

cout << output.i << endl;
}

这个例子很简单,但这并不意味着可以将它简单地转化为一个功能---它对应于一个大项目。因此不可能:

  • 删除已经存在的类或函数(但可以修改它们并创建新的)
  • 使worker静态化或只创建一个worker拷贝(每个worker在许多复杂的函数和循环中都是临时的)

那么如何修改它,使其变成这样:

 // TODO: TProcess declaration

struct TInput1 {
int i;
};
class TWorker1{
public:
void Init( TInput1 i ) { fResult = i; }
void Add( TInput1 i ) { fResult += i.i; }
int Result() { return fResult; }
private:
int fResult;
};
#else
struct TInput2 {
int i;
};
class TWorker2 {
public:
void Init( TInput2 i ) { fResult = i.i; }
void Add( TInput2 i ) { fResult ^= i.i; }
int Result() { return fResult; }
private:
int fResult;
};

void TProcess::Run() {
for( int i = 0; i < fInput.size(); ++i ) {
// TODO: choose and create a worker
worker.Add(fInput[i].i);
// TODO: get and save result
}

fOutput.i = worker.Result();
}

int main() {
vector<TInputBase> input(NInputs);

// TODO: fill input

TProcess proc(input);
proc.Run();

TOutput output;
proc.GetOutput(output);

cout << output.i << endl;
}

我最初的想法是使用基本的类和模板函数,但是没有模板虚函数...

最佳答案

您对 vector<TInputBase> 的想法是正确的第二个示例中的声明——您需要为所有输入设置一个公共(public)基类,对于所有工作人员也类似:

class TInput {
}

class TInput1 : public TInput { ... }
class TInput2 : public TInput { ... }

class TWorker {
public:
void Init(TInput *input) = 0;
void Add(TInput *input) = 0;
int Result() = 0;
}

class TWorker1 : public TWorker { ... }
class TWorker2 : public TWorker { ... }

但是请注意,这意味着所有 worker 只能拿 TInput *作为输入,您需要在每个工作类中转换为正确的输入类。

决定对给定输入使用哪个工作类的最简单方法是询问输入本身!您可以在输入类中有一个虚函数来创建正确类型的 worker:

class TInput {
virtual TWorker *createWorker() = 0;
}

class TInput1 : public TInput {
TWorker *createWorker() {
return new TWorker1();
}
}

class TInput2 : public TInput {
TWorker *createWorker() {
return new TWorker2();
}
}

如果由于某种原因无法做到这一点,您可以使用 typeid 判断输入的类型并创建对应的worker实例。

关于c++ - 自动决定使用哪个类进行数据处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12031746/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com