[C ++将单例函数注册到库中

问题描述 投票:0回答:1

我需要帮助才能将应用程序中的功能注册到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起作用,因为它是普通函数,而不是单例和其他事物,但是来自单例类的函数不起作用。

c++ visual-studio dll
1个回答
0
投票

您将需要将一个成员函数绑定到一个特定的实例,您不能像一个普通的函数指针那样仅仅传递一个成员函数-两者完全不同。成员函数实际上由两部分组成-实例和函数。幸运的是,lambda可以很好地解决此特定问题。

© www.soinside.com 2019 - 2024. All rights reserved.