0
私は一連の数値を生成するコードを用意しており、それらの数値に対して異なるオンライン後処理を行いたいと思います。私はそうのように、これを達成するためのポリシーベースのデザインを使用しようとしています:C++ - テンプレートポリシークラスを使用したオーバーロード
は// This is a general class to integrate some quantity
class QuantityIntegrator
{
public:
QuantityIntegrator() : result(0) {}
double getResult() const {return result;}
void setResult(const double val) {result = val;}
private:
double result;
};
// This is my policy class
// A dummy integrator for this example, but there can be others for
// arithmetic average, root-mean-square, etc...
struct NoIntegrator : public QuantityIntegrator
{
// The function that characterizes a policy
void addValue(double val, double) {setResult(val);}
};
// Interface
// This is needed because I want to create a vector of OutputQuantity, which
// is templated
class OutputQuantity_I
{
public:
// These are the functions that I want to override
virtual double getResult() const {cout << "Calling forbidden function getResult"; return -123456;}
virtual void addValue(double, double) {cout << "Calling forbidden function addValue";}
// A method that produces some number sequence
double evaluate() const
{
return 1;
}
};
// The general class for output quantities, from which concrete output
// quantities will inherit
template <typename IntegratorPolicy>
struct OutputQuantity : public OutputQuantity_I,
public IntegratorPolicy
{
};
// One particular output quantity, whose template I can specialize to decide
// how to integrate it
template <typename IntegratorPolicy>
struct SomeOutput : public OutputQuantity<IntegratorPolicy>
{
};
typedef std::vector<OutputQuantity_I*> OutputQuantityList;
int main()
{
SomeOutput s;
OutputQuantityList l;
l.push_back(&s);
// Here OutputQuantity_I::addValue is called, instead of
// IntegratorPolicy::addValue
l[0]->addValue(1,2);
}
は、だから私の質問がされています。どのように私はIntegratorPolicy
によって定義されたメソッドaddValue
を呼び出すコードを得ることができますか?
p.s.私はC++ 98を使用することになっています。
静的でない関数を静的な方法で呼び出そうとしているようです。 – UKMonkey