Advertisement
ASP_Volume2 Complete Applications #40571

How to use Function Pointers

This article explains the proccesses involved when using function pointers. As well as explaing how and why they are used.

AI

AIサマリー: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

ソースコード
original-source
Introduction Function pointers 
When writing applications in C++, there will come a time in your programming career (very high chance) that you will have to use function pointers. If you have ever written software under the platform SDK you have used function pointers (whether or not your aware of it!). What are function pointers you may ask ? Well function pointers , are a lot like variable pointers. They ‘point’ to some other function. Why would we need to use function pointers you may ask? Well there comes a time when you are going to want to setup a callback function, or have the ability for a class, to be able to point and call a function that is outside of it. A really good example of this , is when creating your windows class in platform SDK. When filling out the 
lpfnWndProc what your really doing is pointing to a function (the callback function for that window) for the messages to be processed. Now that we have a general understanding of function pointers, and why I can simply explain the syntax. 
<br><br>
#include "iostream.h"<br>
// the pointer<br>
void (* ptheFunc) (int x, int y);<br> void TheFunc(int x, int y)<br>
<br>{<br>
cout << &#8220;x: &#8220; << x << &#8220; y: &#8220; << y;<br>
<br>}<br>
<br>void main()<br>
<br>{<br>
pTheFunc = theFunc;<br>
pTheFunc(100,100);<br>
<br>}<br>
<br> <br> 
understanding the code
basicly what we did was have a function pointer, point to and call a function, we created a function pointer of it with the above syntax, and then assigned the function pointer a value and then called it. Function pointers prototypes, must be the same as the function they are pointing to. Well that about wraps it up, good luck on your programming ventures. 
- Jared Bruni
オリジナルのコメント (3)
Wayback Machineから復元