Ich habe einen Code, der eine Sequenz von Zahlen erzeugt, und ich möchte verschiedene Online-Nachbearbeitung für diese Zahlen tun. Ich versuche, eine richtlinienbasierte Design zu verwenden, um dies zu erreichen, etwa so:C++ - Überladen mit Vorlage Policy-Klasse
// 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);
}
Also meine Frage ist: Wie kann ich den Code bekommen die Methode aufzurufen addValue
definiert durch IntegratorPolicy
?
Ps.s. Ich bin verpflichtet, C++ 98 zu verwenden.
Sieht so aus, als ob Sie versuchen, nicht statische Funktionen auf statische Weise aufzurufen. – UKMonkey