我需要帮助才能将应用程序中的功能注册到lib,以后可以在lib中使用它。该函数是单例函数。
这是我想像这样发送的方式:
extern "C" void __declspec(dllexport) RegisterFunctionsLib(const std::function<void(int, const std::string&)>& func_1, const std::function<void(const std::string&)>& func_2);
RegisterFunctionsLib(
CMySingletonClass::Instance().NameFunction, // not works
NormalFunction, // works
);
并且在我的lib项目中看起来像:
void RegisterFunctionsLib(const std::function<void(int, const std::string&)>& func_1, const std::function<void(const std::string&)>& func_2)
{
func_1(1, "func_1");
func_2("func_2");
}
func_2起作用,因为它是普通函数,而不是单例和其他事物,但是来自单例类的函数不起作用。
您将需要将一个成员函数绑定到一个特定的实例,您不能像一个普通的函数指针那样仅仅传递一个成员函数-两者完全不同。成员函数实际上由两部分组成-实例和函数。幸运的是,lambda可以很好地解决此特定问题。