函数指针

看龙书时看到函数指针

 

int d3d::EnterMsgLoop( bool (*ptr_display)(float timeDelta) )
{
    MSG msg;
    ::ZeroMemory(&msg, sizeof(MSG));

    static float lastTime = (float)timeGetTime(); 

    while(msg.message != WM_QUIT)
    {
        if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
        {
            ::TranslateMessage(&msg);
            ::DispatchMessage(&msg);
        }
        else
        {    
            float currTime  = (float)timeGetTime();
            float timeDelta = (currTime - lastTime)*0.001f;

            ptr_display(timeDelta);

            lastTime = currTime;
        }
    }
    return msg.wParam;
}
View Code

 

看下面一个简单的实例就明白了

 

 1 #include "stdafx.h"
 2 # include <cstdio>
 3 bool fun1(float min)
 4 {
 5     printf("%f\n",min);
 6     return 0;
 7 }
 8 void fun(bool (*ptr)(float min))
 9 {
10     ptr(3.14);
11 }
12  
13 void main()
14 { 
15     fun(fun1);
16     getchar();
17 }
View Code

 

 

 

 

 

posted @ 2013-08-09 11:37  迷路君的博客  Views(157)  Comments(0Edit  收藏  举报