X* , then the expression F(x) is equivalent to the expression x->f(). The difference is simply that F can be passed to STL algorithms whose arguments must be function objects.

Mem_fun_t is one of a family of member function adaptors. These adaptors are useful if you want to combine generic programming with inheritance and polymorphism, since, in C++, polymorphism involves calling member functions through pointers or references.

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

Example

struct B {

 virtual void print() = 0;

};

struct D1 : public B {

 void print() { cout << 'I'm a D1' << endl; }

};

struct D2 : public B {

 void print() { cout << 'I'm a D2' << endl; }

};

int main() {

 vector<B*> V;

 V.push_back(new D1);

 V.push_back(new D2);

 V.push_back(new D2);

 V.push_back(new D1);

 for_each(V.begin(), V.end(), mem_fun(&B::print));

}

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_fun_t invokes.
Model of

Adaptable Unary Function

Type requirements

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

Public base classes

unary_function<X*, Result>

Members
Member Where defined Description
argument_type Adaptable Unary Function The type of the argument: X*
result_type Adaptable Unary Function The type of the result: Result
Result operator()(X* x) const Unary Function Function call operator. Invokes x->f(), where f is the member function that was passed to the constructor.
explicit mem_fun_t(Result (X::*f)()) mem_fun_t See below.
template <class Result, class X> mem_fun_t<Result, X> mem_fun(Result (X::*f) ()); mem_fun_t See below.
New members

These members are not defined in the Adaptable Unary Function requirements, but are specific to mem_fun_t.

Member Description
explicit mem_fun_t(Result (X::*f)())
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

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

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