C++
  Home arrow C++ arrow Page 4 - Function Pointers, part 3
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Function Pointers, part 3
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 13
    2005-06-07

    Table of Contents:
  • Function Pointers, part 3
  • Windows Calling Conventions
  • Application of Callback Functions
  • Functors Encapsulate Function Pointers

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Function Pointers, part 3 - Functors Encapsulate Function Pointers


    (Page 4 of 4 )

    Regular function pointers are quite easy to use, but as soon as you start using member function pointers, you need an object to use them on. They are no good to you otherwise.

    You can also store both of them in a Functor and treat that functor object as a replacement function instead.

    How does an object behave like a function? You overload operator () in a polymorphic MyFunctor base class from which we will implement derived functors. This way our main application can deal with MyFunctor without having to know what kind of specific derived implementations we have provided it. You can give it functors with extra debugging functionality or performance enhanced ones etc. The application won’t care; it will just call operator ().

    // the abstract baseclass
    class MyFunctor {
    public:
      virtual ~MyFunctor() {}
      virtual int operator ()(char const *str)=0;
    };

    // derived implementation
    template <class T> class DerFunctor : public MyFunctor {
    public:
      DerFunctor(T* pObj, int (T::*funcPtr)(const char*))
          : m_funcPtr(funcPtr), m_pObject(pObj) {}

      int operator ()(char const *str) {
          return (m_pObject->*m_funcPtr)(str);
      }
    private:
      int (T::*m_funcPtr)(const char*); // member func.ptr.
      T* m_pObject;                     // ptr. to object.
    };

    The constructor of DerFunctor takes a pointer to the object it can use the member function pointer on, which it receives as an argument as well. When we call operator () on an instantiated DerFunctor, it then executes that member function pointer on that object. Easy, isn’t it?

    Here is how we can make good use of this.

    class Dummy1 {
    public:
      int Run(char const *str) { /*...*/ return 0; }
    };

    class Dummy2 {
    public:
      int Run(char const *str) { /*...*/ return 1;
    };

    int main (int argc, char const *argv[]) {
      // here are the objects
      Dummy1 dummy1; 
      Dummy2 dummy2;
      // here are the functors
      DerFunctor<Dummy1> functor1(&dummy1, &Dummy1::Run);
      DerFunctor<Dummy2> functor2(&dummy2, &Dummy2::Run);
      // put them in an array to make it more interesting \
      MyFunctor* vtable[] = { &functor1, &functor2 };

      // make the call
      int res1 = functor1(“this is a test”);
      int res2 = (*vtable[1])(“this is a test”);
    }

    This functor example is extremely simplified and there is a lot more to be discussed. The base class I provided here for example, isn’t really generic and you won’t be able to reuse it really. Since these articles were about function pointers I will save the functors for another one.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek