![]() |
University of Murcia ![]() |
QVFunction< Input, Output > Class Template ReferenceBase class for function objects.
More...
|
This class can be used to create function class types. Objects derived from this class will wrap functions so they can be provided as parameters to other methods or procedures.
These objects can be used as any C++ function, using their overloaded function operator (). This operator calls to the virtual method evaluate, which must be implemented by subclasses of QVFunction to contain the code of the function:
[...] class FactorialFunction: public QVFunction<int, int> { public: int evaluate(const int &value) const { int accum = 1; for (int i = 0; i < value; i++) accum *= i; return accum; } }; [...] int main() { [...] FactorialFunction factorial; std::cout << "10! = " << factorial(10) << std::endl; [...] }
Function objects can contain parametrization data to evaluate the function, besides each given input value:
class QuadraticFunction: public QVFunction<double, double> { private: const double a, b, c; public: QuadraticFunction(const double a, const double b, const double c): QVFunction<double, double>(), a(a), b(b), c(c) { } double evaluate(const double &x) const { return a*x*x + b*x + c; } };
Definition at line 82 of file qvfunction.h.