cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : Miscellaneous : functional : mem_fun_ref
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
functional
iterator
memory
utility
functional
binary_function
unary_function
operator classes:
· divides
· equal_to
· greater
· greater_equal
· less
· less_equal
· logical_and
· logical_not
· logical_or
· minus
· modulus
· multiplies
· negate
· not_equal_to
· plus
adaptor functions:
· bind1st
· bind2nd
· mem_fun
· mem_fun_ref
· not1
· not2
· ptr_fun
types:
· binary_negate
· binder1st
· binder2nd
· const_mem_fun1_ref_t
· const_mem_fun1_t
· const_mem_fun_ref_t
· const_mem_fun_t
· mem_fun1_ref_t
· mem_fun1_t
· mem_fun_ref_t
· mem_fun_t
· pointer_to_binary_funct...
· pointer_to_unary_function
· unary_negate

-

mem_fun_ref class template
template <class S, class T>
  mem_fun_ref_t<S,T> mem_fun_ref (S (T::*f)());

template <class S, class T, class A>
  mem_fun1_ref_t<S,T,A> mem_fun_ref (S (T::*f)(A));

template <class S, class T>
  const_mem_fun_ref_t<S,T> mem_fun_ref (S (T::*f)() const);

template <class S, class T, class A>
  const_mem_fun1_ref_t<S,T,A> mem_fun_ref (S (T::*f)(A) const);
<functional>

Convert member function to function object (reference version)

Returns a function object that encapsulates member function f of type T. The member function returns a value of type S and, optionally, can take one parameter of type A.

The function object returned by this function expects a reference to an object as its (first) argument for operator(). A similar function, mem_fun generates the same function but expecting a pointer to an object as (first) argument instead.

Function objects are objects whose class defines member function operator(). This member function allows the object to be used with the same syntax as a regular function call. Several standard algorithms and adaptors are designed to be used with function objects.

It is defined with the same behavior as:

template <class S, class T>
  mem_fun_ref_t<S,T> mem_fun_ref (S (T::*f)())
  { return mem_fun_ref_t<S,T>(f); }

template <class S, class T, class A>
  mem_fun1_ref_t<S,T,A> mem_fun_ref (S (T::*f)(A))
  { return mem_fun1_ref_t<S,T,A>(f); }

template <class S, class T>
  const_mem_fun_ref_t<S,T> mem_fun_ref (S (T::*f)() const)
  { return const_mem_fun_ref_t<S,T>(f); }

template <class S, class T, class A>
  const_mem_fun1_ref_t<S,T,A> mem_fun_ref (S (T::*f)(A) const)
  { return const_mem_fun1_ref_t<S,T,A>(f); }

Template parameters

S
Return type of member function.
T
Type (class) of which the member function is a member.
A
Type of the argument taken by the member function (if any).

Parameters

f
Pointer to a member function, taking either one argument (of type A) or no arguments, and returning a value of type S.

Return value

A function object which calls the member function f of the object passed as its (first) argument.
If the member function accepts one parameter, this is specified as the second argument in the function object.

Example

// mem_fun_ref example
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

int main () {
  vector<string> numbers;

  // populate vector:
  numbers.push_back("one");
  numbers.push_back("two");
  numbers.push_back("three");
  numbers.push_back("four");
  numbers.push_back("five");

  vector <int> lengths (numbers.size());

  transform (numbers.begin(), numbers.end(), lengths.begin(), mem_fun_ref(&string::length));
	
  for (int i=0; i<5; i++) {
	  cout << numbers[i] << " has " << lengths[i] << " letters.\n";
  }
  return 0;
}

Output:


one has 3 letters.
two has 3 letters.
three has 5 letters.
four has 4 letters.
five has 4 letters.

See also

mem_fun Convert member function to function object (pointer version) (function template)
ptr_fun Convert function pointer to function object (function template)
unary_function Unary function object base class (class template)
binary_function Binary function object base class (class template)

Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us