2017-12-13 14 views
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を使用することになっています。

+0

静的でない関数を静的な方法で呼び出そうとしているようです。 – UKMonkey

答えて

0

私はそれを考えて解決策を見つけました。この問題は実際には愚かであることに気がつきますが、誰かがそれにぶつかるかもしれないので、私はそれを投稿します。内部OutputQuantity私はaddValueに明示的にIntegratorPolicy::addValueを呼び出して、基本的にはラッパー関数を書いています。

関連する問題