polymorphism involves calling member functions through pointers or references.

As with many other adaptors, it is usually inconvenient to use mem_fun1_t's constructor directly. It is usually better to use the helper function mem_fun [2] instead.

Example

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));

}

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters
Parameter Description
Result The member function's return type.
X The class whose member function the mem_fun1_t invokes.
Arg The member function's argument type.
Model of

Adaptable Binary Function

Type requirements

• X has at least one member function that takes a single argument of type Arg and that returns a value of type Result. [1]

Public base classes

binary_function<X*, Arg, Result>

Members
Member Where defined Description
first_argument_type Adaptable Binary Function The type of the first argument: X*
second_argument_type Adaptable Binary Function The type of the second argument: Arg
result_type Adaptable Binary Function The type of the result: Result
Result operator()(X* x, Arg a) const Binary Function Function call operator. Invokes x->f(a), where f is the member function that was passed to the constructor.
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
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату