Function Pointer must pointer to function with definition not just declaration.
Consider the following function:
int f(char a, float b);The type of this function is different depending on whether it is an ordinary function or a non- static member function of some class:
- Its type is "int (*)(char,float)" if an ordinary function
- Its type is "int (Fred::*)(char,float)" if a non-static member function of class Fred
Note: if it's a static member function of class Fred, its type is the same as if it were an ordinary function: "int (*)(char,float)".
Both C and C++ support function pointers, which provide a way to pass around instructions on how to perform an operation. But function pointers are limited because functions must be fully specified at compile time. What do I mean? Let's say that you're writing a mail program to view an inbox, and you'd like to give the user the ability to sort the inbox on different fields--to, from, date, etc. You might try using a sort routine that takes a function pointer capable of comparing the messages, but there's one problem--there are a lot of different ways you might want to compare messages. You could create different functions that differ only by the field of the message on which the comparison occurs, but that limits you to sorting on the fields that have been hard-coded into the program. It's also going to lead to a lot of if-then-else blocks that differ only by the function passed into the sort routine.
What you'd really like is the ability to pass in a third argument to your comparison function, telling it which field to look at. But to make this work, you'd have to write your own sort routine that knows about the third argument; you can't use a generic routine like the STL's sort function because you can't tell it to pass in a third argument to the comparator. Instead, you somehow need the ability to "embed" what field to sort on inside the function.
It turns out that you can get this behavior in C++ (but not in C) through the use of function objects (aka "functors"). Functors are objects that can be treated as though they are a function or function pointer--you could write code that looks like this:
1
2
|
myFunctorClass functor;
functor( 1, 2, 3 );
|
While overloading operator() is nice, the really cool thing about functors is that their lifecycle is more flexible than that of a function--you can use a a functor's constructor to embed information that is later used inside the implementation of operator().
Let's look at an example. This example creates a functor class with a constructor that takes an integer argument and saves it. When objects of the class are "called", it will return the result of adding the saved value and the argument to the functor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <iostream>
class
myFunctorClass
{
public
:
myFunctorClass (
int
x) : _x( x ) {}
int
operator() (
int
y) {
return
_x + y; }
private
:
int
_x;
};
int
main()
{
myFunctorClass addFive( 5 );
std::cout << addFive( 6 );
return
0;
}
|
Sorting Mail
Now we can think about how we'd want to write a mail sorting functor. We'd need operator() to take two messages, and we can have its constructor store the field we wish to sort by.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
class
Message
{
public
:
std::string getHeader (
const
std::string& header_name)
const
;
// other methods...
};
class
MessageSorter
{
public
:
// take the field to sort by in the constructor
MessageSorter (
const
std::string& field) : _field( field ) {}
bool
operator (
const
Message& lhs,
const
Message& rhs)
{
// get the field to sort by and make the comparison
return
lhs.getHeader( _field ) < rhs.getHeader( _field );
}
private
:
std::string _field;
};
|
1
2
3
4
|
std::vector<Messages> messages;
// read in messages
MessageSorter comparator;
sort( messages.begin(), messages.end(), comparator );
|
Functors Compared with Function Pointers
If you have a function that takes a function pointer, you cannot pass in a functor as though it were a function pointer, even if the functor has the same arguments and return value as the function pointer.Likewise, if you have a function that expects a functor, you cannot pass in a function pointer.
Functors, Function Pointers, and Templates
If you wish to allow either a function pointer or a functor to be passed into the same function, you need to use templates. The templated function will deduce the proper type for the functor or function pointer, and both functors and function pointers are used in the exact same way--they both look like function calls--so the code in the function will compile fine.For instance, let's say you had this simple little find_matching_numbers function that takes a vector and returns a vector of all numbers for which a given function returns true.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
std::vector<
int
> find_matching_numbers (std::vector<
int
> vec,
bool
(*pred)(
int
))
{
std::vector<
int
> ret_vec;
std::vector<
int
>::iterator itr = vec.begin(), end = vec.end();
while
( itr != end )
{
if
( pred( *itr ) )
{
ret_vec.push_back( *itr );
}
++itr;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
template
<
typename
FuncType>
std::vector<
int
> find_matching_numbers( std::vector<
int
> vec, FuncType pred )
{
std::vector<
int
> ret_vec;
std::vector<
int
>::iterator itr = vec.begin(), end = vec.end();
while
( itr != end )
{
if
( pred( *itr ) )
{
ret_vec.push_back( *itr );
}
++itr;
}
}
|
Functors vs. Virtual Functions
Functors and virtual functions are, believe it or not, closely related. They both solve the same problem: how do we let some code choose the algorith that is applied. Imagine that you have a function called doMath, and it takes 3 arguments: two integers, and a "kind of math to do". You could write it with functors like this:
1
2
3
4
5
|
template
<FuncType>
int
doMath (
int
x,
int
y, FuncType func)
{
return
func( x, y );
}
|
1
2
3
4
5
6
7
8
9
|
class
MathComputer
{
virtual
int
computeResult (
int
x,
int
y) = 0;
};
doMath (
int
x,
int
y, MathComputer* p_computer)
{
return
p_computer->computeResult( x, y );
}
|
The major difference is that using virtual functions does not give the ability to write a templated function that can also accept function pointers. Depending on your application, this may be important: if you're writing a library, it's probably very important; if you're writing a small program for yourself, it's likely less important. A virtual function, on the other hand, has somewhat simpler syntax (no complex templates!) and tends to fit the normal object-oriented programming mindset.