Categories: functors, adaptors | Component type: type |
mem_fun_ref_t<Result, X>'s constructor takes a pointer to one of X's member functions. Then, like all function objects, mem_fun_ref_t has an operator() that allows the mem_fun_ref_t to be invoked with ordinary function call syntax. In this case, mem_fun_ref_t's operator() takes an argument of type X&.
If F is a mem_fun_ref_t that was constructed to use the member function X::f, and if x is of type 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_ref_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. In fact, though, mem_fun_ref_t is usually not as useful as mem_fun_t. The difference between the two is that mem_fun_t's argument is a pointer to an object while mem_fun_ref_t's argument is a reference to an object. References, unlike pointers, can't be stored in STL containers: pointers are objects in their own right, but references are merely aliases.
As with many other adaptors, it is usually inconvenient to use mem_fun_ref_t's constructor directly. It is usually better to use the helper function mem_fun_ref instead.
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<D1> V; V.push_back(D1()); V.push_back(D1()); for_each(V.begin(), V.end(), mem_fun_ref(&B::print)); }
Parameter | Description | Default |
---|---|---|
Result | The member function's return type. | |
X | The class whose member function the mem_fun_ref_t invokes. |
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_ref_t(Result (X::*f)()) |
mem_fun_ref_t | See below. |
template <class Result, class X> mem_fun_ref_t<Result, X> mem_fun_ref(Result (X::*f)()); |
mem_fun_ref_t | See below. |
Member | Description |
---|---|
explicit mem_fun_ref_t(Result (X::*f)()) |
The constructor. Creates a mem_fun_ref_t that calls the member function f. |
template <class Result, class X> mem_fun_ref_t<Result, X> mem_fun_ref(Result (X::*f)()); |
If f is of type Result (X::*)() then mem_fun_ref(f) is the same as mem_fun_ref_t<Result, X>(f), but is more convenient. This is a global function, not a member function. |
[1] The type Result is permitted to be void. That is, this adaptor may be used for functions that return no value. However, this presents implementation difficulties. According to the draft C++ standard, it is possible to return from a void function by writing return void instead of just return. At present, however (early 1998), very few compilers support that feature. As a substitute, then, mem_fun_ref_t uses partial specialization to support void member functions. If your compiler has not implemented partial specialization, then you will not be able to use mem_fun_ref_t with member functions whose return type is void.
Contact Us | Site Map | Trademarks | Privacy | Using this site means you accept its Terms of Use |
Copyright © 1993-2006 Silicon Graphics, Inc. All rights reserved. |