将非静态函数指针传递给另一个类
Pass a non-static function pointer to another class?
我有两个类,Win32和引擎。我试图将我的WindowProc函数从我的引擎类传递到Win32类。我知道类型定义WNDPROC被声明为:
typedef LRESULT(CALLBACK *WNDPROC)(HWND, UINT, WPARAM, LPARAM);
我的Win32标头声明为:
// Win32.h
#include <Windows.h>
class Win32
{
public:
Win32() {};
~Win32() {};
void Initialize(WNDPROC);
private:
// Route messages to non static WindowProc that is declared in Engine class.
static LRESULT CALLBACK MessageRouter(HWND, UINT, WPARAM, LPARAM);
};
我的Engine类被声明为:
// Engine.h
#include "Win32.h"
class Engine
{
public:
Engine() {};
~Engine() {};
void Initialize();
private:
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
Win32* m_win32;
};
// Engine.cpp
#include "Engine.h"
void Engine::Initialize()
{
m_win32 = new Win32;
m_win32->Initialize(&WindowProc); // How can I pass this function without making
// it static or global.
}
我的Win32类已经有一个静态MessageRouter给WNDCLASSEX。所以我的问题是,我如何传递引擎::WindowProc函数类Win32不声明它的静态或全局?
您可以使用std::function
和std::bind()
(在c++ 11中),或boost::function
boost::bind()
(在c++ 03中)。这两者在功能上是相当的,所以我将展示std::bind()
的用法。
std::function
定义一个名为WNDPROC_FXN
的类型别名:
typedef std::function<LRESULT CALLBACK (HWND, UINT, WPARAM, LPARAM)> WNDPROC_FXN;
这就是你在Win32
类中使用它的方式:
class Win32
{
public:
Win32() {};
~Win32() {};
void Initialize(WNDPROC_FXN);
// ^^^^^^^^^^^
private:
// Route messages to non static WindowProc that is declared in Engine class.
static LRESULT CALLBACK MessageRouter(HWND, UINT, WPARAM, LPARAM);
};
这就是将成员函数绑定到this
指针并传递给Win32::Initialize()
的方法:
#include <functional>
// ...
void Engine::Initialize()
{
using namespace std::placeholders;
m_win32 = new Win32;
m_win32->Initialize(std::bind(&Engine::WindowProc, this, _1, _2, _3 _4);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
为了完整起见,还有一种方法可以使用语言结构来完成此操作。该实现使用指向成员函数的指针:
// Win32.h
#include <Windows.h>
class Engine;
class Win32
{
public:
Win32() {};
~Win32() {};
void Initialize(LRESULT(CALLBACK Engine::* function)(HWND, UINT, WPARAM, LPARAM));
private:
// Route messages to non static WindowProc that is declared in Engine class.
static LRESULT CALLBACK MessageRouter(HWND, UINT, WPARAM, LPARAM);
};
class Engine
{
public:
Engine() {};
~Engine() {};
void Initialize();
private:
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
Win32* m_win32;
};
void Engine::Initialize()
{
m_win32 = new Win32;
m_win32->Initialize(&Engine::WindowProc); // How can I pass this function without making
// it static or global.
}
int main(void)
{
Engine engine;
engine.Initialize();
return 0;
}
相关文章:
- 从另一个静态常量数组初始化静态常量数组(只需少量计算)
- 将值传递到另一个类函数在打印时为零
- 如果两个静态库使用相同的另一个静态库,如何避免"LNK2005 Already Defined error"?
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 如何处理没有默认构造函数但在另一个构造函数中构造的对象?
- 如何将一个 void 函数传递给另一个 void 函数?
- 类函数返回到另一个类函数返回打印不同的值
- 无法将指向类的成员函数的函数指针作为参数传递给同一类的另一个成员函数
- 在模板化的 Rcpp 函数中调用另一个 cpp 函数
- C++ 如何根据作为输入传递的参数调用一个构造函数或另一个构造函数?
- 使用线程从另一个成员函数调用一个Member函数
- 如何通过指针将模板成员函数传递给另一个成员函数
- 在另一个成员函数中调用成员函数时'int'之前的预期主表达式
- C 将成员函数作为参数传递给另一个成员函数
- 如何在另一个成员函数中修改具有常量返回类型的成员函数的返回值
- 让构造函数在其初始化列表中调用同一类的另一个构造函数是否有效
- C++ 给出编译器错误,将一个(静态)常量全局变量设置为另一个静态常量变量
- 将对静态存储 cstyle 数组的引用从 constexpr 函数传递到另一个 constexpr 函数
- 为什么 Clang++ 不在另一个静态库中运行全局对象构造函数?
- 如何在C++中从静态函数内部将指向静态函数的指针作为参数传递给另一个静态函数