polymorphism involves calling member functions through pointers or references.
As with many other adaptors, it is usually inconvenient to use
struct Operation {
virtual double eval(double) = 0;
};
struct Square : public Operation {
double eval(double x) { return x * x; }
};
struct Negate : public Operation {
double eval(double x) { return –x; }
};
int main() {
vector<Operation*> operations;
vector<double> operands;
operations.push_back(new Square);
operations.push_back(new Square);
operations.push_back(new Negate);
operations.push_back(new Negate);
operations.push_back(new Square);
operands.push_back(1);
operands.push_back(2);
operands.push_back(3);
operands.push_back(4);
operands.push_back(5);
transform(operations.begin(), operations.end(), operands.begin(), ostream_iterator<double>(cout, '
'), mem_fun(Operation::eval));
}
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
Result | The member function's return type. |
X | The class whose member function the |
Arg | The member function's argument type. |
Adaptable Binary Function
binary_function<X*, Arg, Result>
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Function | The type of the first argument: |
second_argument_type | Adaptable Binary Function | The type of the second argument: |
result_type | Adaptable Binary Function | The type of the result: |
Result operator()(X* x, Arg a) const | Binary Function | Function call operator. Invokes |
explicit mem_fun1_t(Result (X::*f)(Arg)) | mem_fun1_t | See below. |
template <class Result, class X, class Arg> mem_fun1_t<Result, X, Arg> mem_fun (Result (X::*f)(Arg)); [2] | mem_fun1_t |