将对象的成员函数存储为另一个对象中的回调函数

时间:2013-07-28 14:48:07

标签: c++ winapi timer callback function-pointers

我是回调功能的新手。我写了一个Timer课程。它将回调函数存储在其中,并在各自的句号后调用它们。

Timer.h

#ifndef UTILITY_CLASSES__TIMER_H
#define UTILITY_CLASSES__TIMER_H

#include <stdint.h>
#include <list>
#include <Windows.h>

class Timer
{
    public:
        static uint64_t LAST_TIMER_ID;
        uint64_t TimerId;
        uint64_t KillId;
        typedef void (*TimerCallBackFunction) (DWORD, uint64_t);

        Timer(UINT Period, TimerCallBackFunction CbFunction);
        ~Timer();

    protected:
        class TimerInfo
        {
            public:
                TimerInfo(TimerCallBackFunction Cbf, uint64_t Tid, uint64_t Kid)
                    : Callback(Cbf), TimerId(Tid), KillId(Kid) {}
                TimerCallBackFunction Callback;
                uint64_t TimerId;
                uint64_t KillId;
        };

        static VOID CALLBACK TimerProc( _In_  HWND hwnd,
                                        _In_  UINT uMsg,
                                        _In_  UINT_PTR idEvent,
                                        _In_  DWORD dwTime);

        static std::list<TimerInfo> Timers;
};

#endif  // UTILITY_CLASSES__TIMER_H

Timer.cpp

#include "Timer.h"

#include <Windows.h>

uint64_t Timer::LAST_TIMER_ID = 0;

Timer::Timer(UINT Period, TimerCallBackFunction CbFunction)
{
    TimerId = ++LAST_TIMER_ID;
    KillId = SetTimer(NULL, TimerId, Period, Timer::TimerProc);
    if (KillId == NULL)
    {
        LAST_TIMER_ID--;    // roll back
        TimerId = 0;
    } 
    else
    {
        Timers.push_back(TimerInfo(CbFunction, TimerId, KillId));
    }
}

Timer::~Timer()
{
    for (std::list<TimerInfo>::const_iterator it=Timers.begin(); it!=Timers.end(); ++it)
    {
        if (it->TimerId == TimerId)
        {
            Timers.erase(it);
            break;
        }
    }
}

VOID CALLBACK Timer::TimerProc( _In_ HWND hwnd,
                                _In_ UINT uMsg,
                                _In_ UINT_PTR idEvent,
                                _In_ DWORD dwTime)
{
    for (std::list<TimerInfo>::const_iterator it=Timers.begin(); it!=Timers.end(); ++it)
    {
        if (it->TimerId == idEvent)
        {
            it->Callback(dwTime, it->TimerId);
            break;
        }
    }
}

的main.cpp

#include <Windows.h>
#include "Timer.h"

class MyClass
{
    public:
        void CallMe(DWORD Time, uint64_t TimerId)
        {
            MessageBoxW(NULL, L"Callback function is called!",
                        L"Notification", MB_ICONINFORMATION);
        }
};

int APIENTRY wWinMain(  _In_        HINSTANCE   hInstance,
                        _In_opt_    HINSTANCE   hPrevInstance,
                        _In_        LPTSTR      lpCmdLine,
                        _In_        int         nCmdShow)
{
    // ...

    MyClass MyObject;
    Timer TimerObject(10000, MyObject.CallMe);  // Line 114

    //...
    return 0;
}

编译此代码时收到此错误消息:

  

第114行   错误C3867:
  'MyClass :: CallMe':函数调用缺少参数列表;使用'&amp; MyClass :: CallMe'创建指向成员的指针

我在第114行将MyObject.CallMe更改为&MyClass::CallMe&MyObject.CallMe时无法运行我的代码。

我的代码中出错了什么?


IDE:Microsoft Visual Studio 2012
文件:SetTimerTimerProc

2 个答案:

答案 0 :(得分:2)

typedef void (*TimerCallBackFunction) (DWORD, uint64_t);

声明一个指向free函数的指针的typedef,这些指针不能用于指向成员函数。你需要一个指向成员函数的指针:

typedef void (MyClass::*TimerCallBackFunction) (DWORD, uint64_t);

在创建typedef之前,您需要转发声明MyClass并执行错误消息所说的内容:

Timer TimerObject(10000, &MyObject::CallMe);
                      // ^^^^^^^^^^^^^^^^^

推荐阅读:Pointers to member functions section on C++ FAQ Lite.

答案 1 :(得分:1)

我会使用std::function而不是函数指针,然后将std::bind与占位符一起使用:

将typedef更改为

    typedef std::function<void (DWORD, uint64_t)> TimerCallBackFunction;

并在WinMain()中绑定实例std::function的{​​{1}}到CallMe(),然后使用两个参数替换占位符来调用它。

MyObject