Function Pointers

As someone pointed out, I can learn a lot about something by making a separate topic just about the thing I'm trying to learn...which happens to be function pointers.

Could someone please explain what they do and possibly give a small example program for each?
Did you take a look at this?
http://en.wikipedia.org/wiki/Function_pointer
I did do some research before posting like the welcome post says I was supposed to, and I have seen this page. However, I would mainly like to know the difference between the c and c++ ways of function pointers/functionals.
the difference between the c and c++ ways

That's a rather large topic.

The C way is the way of the function pointer, which is, well, a pointer (internally, an integer or a few integers, on some architectures). A function pointer can be copied and it can be invoked, by applying the function call operator (the pair of round parentheses with arguments) to it on the right.

The C++ way is the way of the function object, which is also something that can be copied and invoked by applying the function call operator, but, unlike the function pointer, it may hold state: that is, invoking it not only executes the code but also changes the object's member variables, so that the next time the same function object is invoked, it may do something different.

Examples of function objects are:

user-defined objects of any type with operator() defined
function pointers (C-style function pointer can be used whenever C++ expects a function object)
references to functions (wrapped with std::ref() to be copyable)
bind expressions (that's what std::bind() returns)
closure objects (that's what lambda-expression returns)
the objects returned by std::mem_fn (wrapped member functions)
the objects returned by std::plus, std::less, and many other standard library function objects
std::function objects that wrap any of the above
Last edited on
So the tutorial on this website is for the "C way"?
A function pointer is a pointer to a function. Basically, it works in a similar way pointers to data do. They are mostly used for callback functions in native code. When you declare a pointer to a function, you must do so with the proper calling convention and the return and arguments as the function you're calling back to.
The syntax can be fairly tricky, but it makes sense if you're intuitive.
Using typedefs can make the syntax much simpler, especially when pointers to functions are arguments or return types.

1
2
3
4
5
6
7
8
9
10
11
12
13
int foo(char c, float f) //callback function
{
    return 0;
}

int main()
{
    int (*callback)(char, float);
    callback= &foo;
    (*callback)('c', 29.5); //call
    //callback('b', 22.2); //implicitly dereferences if your compiler isn't from stone-age
    return 0;
}


Likewise, pointers to static member functions work the same way.
Pointers to member functions that are not static have the implicit this pointer. You must declare and dereference them properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct Foo
{
	void foofunc()
	{
	}

	static void sfoofunc()
	{
	}
};


int main()
{
	Foo instance;
	Foo *pinstance= new Foo;
	void (*scallback)() = &Foo::sfoofunc;
	void (Foo::*memcallback)() = &Foo::foofunc;
	(*scallback)(); //no this, call normally
	(instance.*memcallback)();
	(pinstance->*memcallback)();
	delete pinstance;

	return 0;
}


Never, ever, ever cast a function pointer to another type. Ever.


More information can be found in the C++FAQ
http://www.parashift.com/c++-faq/pointers-to-members.html
Last edited on
Topic archived. No new replies allowed.