如何在 C++/CLI 中使用 boost::bind 绑定托管类的成员
我在本机 C++ 类中使用 boost::signal,现在我正在 C++/CLI 中编写 .NET 包装器,以便可以将本机 C++ 回调公开为 .NET 事件。 当我尝试使用 boost::bind 获取托管类的成员函数的地址时,出现编译器错误 3374,指出除非创建委托实例,否则无法获取成员函数的地址。 有谁知道如何使用 boost::bind 绑定托管类的成员函数?
为了澄清起见,以下示例代码会导致编译器错误 3374:
#include <boost/bind.hpp>
public ref class Managed
{
public:
Managed()
{
boost::bind(&Managed::OnSomeEvent, this);
}
void OnSomeEvent(void)
{
}
};
I am using boost::signal in a native C++ class, and I now I am writing a .NET wrapper in C++/CLI, so that I can expose the native C++ callbacks as .NET events. When I try to use boost::bind to take the address of a member function of my managed class, I get compiler error 3374, saying I cannot take the address of a member function unless I am creating a delegate instance. Does anyone know how to bind a member function of a managed class using boost::bind?
For clarification, the following sample code causes Compiler Error 3374:
#include <boost/bind.hpp>
public ref class Managed
{
public:
Managed()
{
boost::bind(&Managed::OnSomeEvent, this);
}
void OnSomeEvent(void)
{
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您的答案有效时,它会将您的一些实现公开给世界(Managed::OnSomeEvent)。 如果您不希望人们随意通过调用 OnSomeEvent() 来引发 OnChange 事件,您可以按如下方式更新您的托管类(基于 此建议):
请注意所使用的备用
bind()
形式。While your answer works, it exposes some of your implementation to the world (Managed::OnSomeEvent). If you don't want people to be able to raise the OnChange event willy-nilly by invoking OnSomeEvent(), you can update your Managed class as follows (based on this advice):
Note the alternate
bind<R>()
form that's used.经过更多谷歌搜索后,我终于找到了 不错关于如何执行此操作的博客文章。 该帖子中的代码比我需要的要多一点,但主要内容是使用一个全局自由函数,该函数接受封装在 gcroot<> 中的托管 this 指针的参数。 模板。 有关示例,请参阅下面代码中的 SomeEventProxy(...)。 然后,该函数返回并调用我试图绑定的托管成员。 我的解决方案如下所示,以供将来参考。
After googling some more, I finally found a nice blog post about how to do this. The code in that post was a little more than I needed, but the main nugget was to use a global free function that takes an argument of the managed this pointer wrapped in a gcroot<> template. See the SomeEventProxy(...) in the code below for an example. This function then turns around and calls the managed member I was trying to bind. My solution appears below for future reference.