Click here to Skip to main content
11,413,961 members (76,963 online)
Click here to Skip to main content

Pointers to Member Functions and Functors

, 19 May 2004
Rate this:
Please Sign up or sign in to vote.
Using pointers to member functions to build a functor template binding an instance of an object and a member function.

Introduction

This is a brief discussion of C++ 'Pointers to Functions' and 'Pointers to Member Functions', followed by an example of building a functor template to associate an instance of a class and a pointer to a member function of that class into a transferable object. This can be used as a predicate by STL algorithms or other generic code.

Background

A question was posted on the CodeProject Visual C++ forum that led me to researching pointers to members. Subsequently, I've tidied the code snippet I presented in that thread into a more generic form and considered some more typical uses.

Pointers to Member Functions

A pointer to a plain old function is declared (for example) thus,

bool (*pfn)( int )

pfn is a pointer to a function taking a single integer argument and returning a bool. An example usage is:

bool f1 ( int i )
{
    return i > 10 ;
}

int main ()
{
    bool (*pfn)(int) ;
    pfn = f1 ; 
    if ( (*pfn)( 11 ))
        std::cout << "pfn ( 11 ) true\n" ;

    return 0 ;
}

The value of pfn becomes an actual function address as understood by the CPU.

A pointer to a member function is a slightly different animal,

bool (Foo::*pmfn)(int) ;

pmfn is a pointer to a member function of class Foo that takes a single integer argument and returns a bool.

class Foo
{
private :
    int t_ ;
public :
    Foo ( int t ) : t_ ( t )
    {
    }
    bool f1 ( int i )
    {
        return i > t_ ;
    }
} ;

int main ()
{
    bool (Foo::*pmfn)(int) ;
    pmfn = &Foo::f1 ;

    Foo foo ( 10 ) ;
    if ( (foo.*pmfn)( 11 ))
        std::cout << "pmfn ( 11 ) true\n" ;

    return 0 ;
}

This is obviously a pointless piece of code but it suffices to show the syntax. pmfn can be set to any member function of Foo that matches the signature of an int argument and bool return. The actual implementation of pointers to members depends on whether the class has virtual functions, in which case the pointer must be a combination of the address of the relevant table of virtual function pointers and an offset into it. For a plain old class, the pointer is probably a conventional function pointer. The details are implementation and architecture dependent.

Functors

A functor is an instance of a class or struct that can be treated syntactically as a function.

struct PlusFunctor
{
    int operator()( int l, int r )
    {
        return l + r ;
    }
} ;

int main ()
{
    PlusFunctor f ;
    std::cout << "f ( 3, 4 ) = " << f ( 3, 4 ) << std::endl ;

    return 0 ;
}

Function Adaptors

A function adaptor is a functor that allows functions or functors to be combined, or to have arguments bound. One interesting class of function adaptors provided by the STL allows member functions of classes to be called:

class SomeObject
{
private:
    int value_ ;
public :
    SomeObject () : value_ ( 0 )
    {
    }
    SomeObject ( int value ) : value_ ( value )
    {
    }
    void ShowValue ()
    {
        std::cout << value_ << " " ;
    }
} ;

int main ()
{
    std::vector<SomeObject> v ;
    v.push_back ( SomeObject ( 1 )) ;
    v.push_back ( SomeObject ( 2 )) ;
    v.push_back ( SomeObject ( 3 )) ;
    v.push_back ( SomeObject ( 4 )) ;
    std::for_each ( v.begin (), v.end (), 
      std::mem_fun_ref ( &SomeObject::ShowValue )) ;

    return 0 ;
}

This is calling the nominated member function for each object in the collection in a reasonably readable and concise manner. But what if we wanted to call a member function of some other object for each object in a collection? A scenario might be applying a list of updates to a document:

class Update
{
private :
    // update data
public :
    Update () {}
    // access functions
} ;

class Document
{
private :
    // document data
public :
    Document () {} ;

    void ApplyUpdate ( const Update& update )
    {
    }
    // other document functions
} ;

There are two obvious approaches to applying the updates, hand-code a loop to enumerate the container:

template <typename Container> 
  void ApplyUpdatesToDocument1 ( Document& doc, Container& updates )
{
    Container::iterator it = updates.begin () ;
    while ( it != updates.end ())
    {
        doc.ApplyUpdate ( *it ) ;
        ++it ;
    }
}

By making the type of the container a template parameter, we can choose to use a vector, a list, a stack etc. The compiler can deduce the type of the container when you make the call:

int main ()
{
    std::vector<Update> vu ;
    vu.resize ( 10 ) ;
    Document doc ;

    ApplyUpdatesToDocument1 ( doc, vu ) ;
    
    return 0 ;
}

However, it seems inelegant to have to write our own loop when for_each is just sitting there waiting to help. One way to bring for_each into action is to create a custom functor that keeps a reference to the document:

struct ApplyUpdateFunctor
{
    Document& doc_ ;
    ApplyUpdateFunctor ( Document& doc ) : doc_ ( doc )
    {
    }
    void operator () ( const Update& update )
    {
        doc_.ApplyUpdate ( update ) ;
    }
} ;

template < typename Container> 
  void ApplyUpdatesToDocument2 ( Document& doc, Container& updates )
{
    std::for_each ( updates.begin (), 
      updates.end (), ApplyUpdateFunctor ( doc )) ;
}

This is great except that we have to write a functor for each type of operation, store them somewhere, and remember what they do when we come back to the code in a week, month or year.

The Meat of the Biscuit

With some template magic, it's possible to write a generic functor used like this:

template <typename Container> 
  void ApplyUpdatesToDocument3 ( Document& doc, Container& updates )
{
    std::for_each ( updates.begin (), updates.end (), 
       mem_fun_bind1 ( doc, &Document::ApplyUpdate )) ;
}

The call looks more complex than ApplyUpdatesToDocument2 but it requires no custom functor to be defined elsewhere, and the action is written directly in the for_each call. We want to call doc.ApplyUpdate() for each update. Later, maintenance should hopefully require less trawling through other files.

Here is the generic functor code used above:

template <typename C, typename Arg, typename Result>
struct mem_fun_bind1_t : public std::unary_function<Arg, Result>
{
    Result (C::*pmf_ )( Arg ) ;
    C& rC_ ;

    explicit mem_fun_bind1_t ( C& rC, 
       Result (C::*pmf)( Arg )) : rC_ ( rC ), pmf_ ( pmf )
    {
    }
    Result operator () ( Arg a )
    {
        return (rC_.*pmf_) ( a ) ;
    }
} ;

template <typename C, typename Arg, 
  typename Result> mem_fun_bind1_t<C, Arg, Result>
mem_fun_bind1 ( C& c, Result (C::*fn)( Arg ))
{
    return mem_fun_bind1_t <C, Arg, Result>( c, fn ) ;
}

The helper function mem_fun_bind1 generates the functor mem_fun_bind1_t. This allows the compiler to deduce the template parameters and simplifies what has to be written by hand. mem_fun_bind1_t is directly analogous to the ApplyUpdateFunctor shown above with the addition of a pointer to the appropriate member function and conversion to a template to allow reuse with different objects and members.

An equivalent functor for binary functions, with two arguments:

template <typename C, typename Arg1, typename Arg2, typename Result>
struct mem_fun_bind2_t : public std::binary_function<Arg1, Arg2, Result>
{
    Result (C::*pmf_ )( Arg1, Arg2 ) ;
    C& rC_ ;

    explicit mem_fun_bind2_t ( C& rC, 
      Result (C::*pmf)( Arg1, Arg2 )) : rC_ ( rC ), pmf_ ( pmf )
    {
    }
    Result operator () ( Arg1 a1, Arg2 a2 )
    {
        return (rC_.*pmf_) ( a1, a2 ) ;
    }
} ;

template <typename C, typename Arg1, typename Arg2, typename Result>
mem_fun_bind2_t<C, Arg1, Arg2, Result>
mem_fun_bind2 ( C& c, Result (C::*fn)( Arg1, Arg2 ))
{
    return mem_fun_bind2_t <C, Arg1, Arg2, Result>( c, fn ) ;
}

This binary version would suit the poster of the original std::sort question. Extensions to more (or fewer) arguments, if you can find a use for them, is left as an exercise for the reader.

Build Notes

All code in this article has been built and tested using Visual C++ Version 7.1. This obviously requires the addition of the appropriate include files for iostream, vector, algorithm and functional.

There are no files to download, I leave the choice of name for a header file containing the functors and helper functions up to you.

Afterword

This is the first time I've come face to face with member function pointers. Investigating them, writing and testing the code snippets for this article have been educational. I'll be using mem_fun_bind in my work in the future. I hope this article proves of some interest even to those who find templates and the STL offensive to the eye.

History

Written and posted - May 19-20, 2004.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Paul Ranson
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionHow to implement pointers 2 member functions in MFC? Pin
Member #3876438, 14-Mar-07 21:46
memberMember #387643814-Mar-07 21:46 
Generaluse of mem_fun_ref with a const member function Pin
darietti, 11-May-05 6:46
memberdarietti11-May-05 6:46 
Generalcannot convert from 'int (__stdcall CDriverApp ... Pin
mohsen nourian, 4-Mar-05 21:22
membermohsen nourian4-Mar-05 21:22 
GeneralRe: cannot convert from 'int (__stdcall CDriverApp ... Pin
Paul Ranson, 5-Mar-05 11:29
memberPaul Ranson5-Mar-05 11:29 
Generaluse with VC6 Pin
MHOEK, 15-Nov-04 0:11
memberMHOEK15-Nov-04 0:11 
GeneralRe: use with VC6 Pin
Paul Ranson, 15-Nov-04 2:51
memberPaul Ranson15-Nov-04 2:51 
GeneralRe: use with VC6 Pin
MHOEK, 15-Nov-04 4:04
memberMHOEK15-Nov-04 4:04 
GeneralExcellent Pin
waxie, 29-Aug-04 23:24
memberwaxie29-Aug-04 23:24 
GeneralBoost will help you... Pin
Anonymous, 25-May-04 22:51
sussAnonymous25-May-04 22:51 
Generalthe question Pin
bryce, 20-May-04 16:16
memberbryce20-May-04 16:16 
GeneralRe: the question Pin
Paul Ranson, 21-May-04 1:26
memberPaul Ranson21-May-04 1:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.2 | Last Updated 20 May 2004
Article Copyright 2004 by Paul Ranson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid