Home arrow C++ arrow Page 4 - Function Pointers, part 2

Function Pointers, part 2

In the previous article, Jun Nakamura introduced you to the use of regular function pointers, but when you write C++ code, you will be interested in C++ class member function pointers too. It is time to look at how to declare pointers to the members of classes you write for your applications.

Author Info:
By: J. Nakamura
Rating: 4 stars4 stars4 stars4 stars4 stars / 11
May 31, 2005
  1. · Function Pointers, part 2
  2. · Defining and Using C Member Function Pointers
  3. · C Member Function Pointers and Polymorphism
  4. · The Member Function Pointer as Argument and Return Value

print this article

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.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- 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++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials