C++
  Home arrow C++ arrow Page 4 - Function Pointers, part 2
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 
Sun Developer Network 
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 2
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 8
    2005-05-31

    Table of Contents:
  • Function Pointers, part 2
  • Defining and Using C Member Function Pointers
  • C Member Function Pointers and Polymorphism
  • The Member Function Pointer as Argument and Return Value

  • 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 2 - The Member Function Pointer as Argument and Return Value


    (Page 4 of 4 )

    It is very well possible that you want to pass a member function pointer to a function or return one from a function. We have seen that all we need to do is add the ‘MyClass::’ scope in front of the variable name, so lets look at how this translates to function parameters and return values. Let’s place the example function we’ve used before into class MyClass:

    int MyClass::func(char const *param);

    Here are some functions that accept a pointer to this member function:

    // a raw declaration
    void foo(int (MyClass::*FPtr)(char const*));

    // using a typedef
    typedef int (MyClass::*FuncPtr)(char const*);
    void foo(FuncPtr fptr);

    If you compare these function declarations with the ones in the previous article, you will see that indeed all you need to add is ‘MyClass::’. Declaring a function that returns a function pointer, we do the exact same thing:

    // a raw declaration
    int (MyClass::*getFuncPtr1(int ID))(char const*);

    // using a typedef
    typedef int (MyClass::*FuncPtr)(char const*);
    FuncPtr GetFuncPtr2(int ID);

    I honestly think that the raw declaration above (possibly combined with a couple of function pointers as parameters) is a good candidate for usage in an obfuscated C++ coding contest.

    Do you understand the following function declaration?

    char const* const ( MyClass::*FuncAt(
        void (MyClass::*Fptr1)(char, char),
        float (MyClass::*Fptr2)(int) ))(char const*, int);

    Lets not even go there ;-) !

    Member Function Pointer Arrays

    Similar to the previous member function pointers described before; in order to create member function pointer arrays, we only need to add the class scope to the regular function pointer array declaration.

    // a raw declaration
    int (MyClass::*funcArray1[10])(char const*);

    // using a typedef
    typedef int (MyClass::*FuncPtr)(char const*);
    FuncPtr funcArray2[10];

    // initializing ome
    funcArray1[0] = &MyClass::func;

    // and finally making use of it
    MyClass myClass;
    int result1 = (MyClass.*funcArray1[0])(“a test”);

    Now that we have seen the many forms function pointers can take, it is quite understandable that many C++ programming books treat this topic superficially. I hope I was able to shed some light in this rather shaded corner of the language and in the concluding article, I will show you the effect calling conventions have on function pointers, provide you with an example and introduce you to C++ functors.


    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

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT