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.
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?
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)(char const*);
// using a typedef typedef int (MyClass::*FuncPtr)(char const*); FuncPtr funcArray2;
// and finally making use of it MyClass myClass; int result1 = (MyClass.*funcArray1)(“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.