gpt4 book ai didi

c++ - WRL : how to subscribe to events *without* using lambdas?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:55:55 25 4
gpt4 key购买 nike

在 C++/CX 中订阅事件是这样的:

listener::ConnectionReceived +=
ref new TypedEventHandler<StreamSocketListener^, StreamSocketListenerConnectionReceivedEventArgs^>(this, &MyClass::OnConnectionReceived);

我找到的关于如何在 WRL 中订阅事件的所有文档都显示了使用 lambda 表达式的示例,如下所示:

auto connectionReceivedHandler = Callback<ITypedEventHandler<StreamSocketListener*, StreamSocketListenerConnectionReceivedEventArgs*>>
([&] (IStreamSocketListener* cbListener, IStreamSocketListenerConnectionReceivedEventArgs* args)
{
this->doSomething();
});
hr = listener->add_ConnectionReceived(connectionReceivedHandler.Get(), &this->connectionReceivedToken);

但是我如何订阅 WRL 中的事件并提供类方法而不是 lambda?像这样:

hr = listener->add_ConnectionReceived(&MyClass::OnConnectionReceived, &this->connectionReceivedToken);

最佳答案

我不熟悉 WRL,但由于它支持 C++11 lambda,我相信它也应该支持 std::bind :

auto callback = Callback<ITypedEventHandler<StreamSocketListener*,
StreamSocketListenerConnectionReceivedEventArgs*>>
(std::bind(
&MyClass::OnConnectionReceived,
ptr_to_instance_of_MyClass, // eg. this
std::placeholders::_1, // cbListener
std::placeholders::_2 // args
));

hr = listener->add_ConnectionReceived(callback.Get(), &this->connectionReceivedToken);

关于c++ - WRL : how to subscribe to events *without* using lambdas?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19148963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com