Home Forums C Programming Pointers to Member Functions

Viewing 1 reply thread
  • Author
    Posts
    • #1869
      niklaesh
      Participant

      Hello i come across very good tutorial, i think u must check it.

      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,

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

      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,

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

      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.

      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:

      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:

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

      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:

      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:

      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:

      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:

      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:

      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.

    • #3119
      will
      Participant

      Ahsun that was a nice tutorial.
      But it would be very best if you mention the website address from where you get that tuorial so that the users can have a look at it & may benifit from that site also.

      Any how Good work. Whenever you find any good tutorial please do post them here.

      Thanks
      Saqib

Viewing 1 reply thread
  • The forum ‘C Programming’ is closed to new topics and replies.