我正在使用 TRI DDS - 这是我尝试调用的函数的原型:
template<typename T , typename Functor >
dds::sub::cond::ReadCondition::ReadCondition (
const dds::sub::DataReader< T > & reader,
const dds::sub::status::DataState & status,
const Functor & handler
)
所以我有一个看起来有点像这样的类(省略了一些不相关的内容):
MyClass test{
public:
test(){... mp_reader = ...}; // not complete
start_reader()
{
dds::sub::cond::ReadCondition rc(*mp_reader,
dds::sub::status::DataState::any(),
do_stuff()); // This does not work
}
void do_stuff() {...}
private:
dds::sub::DataReader* mp_reader;
}
所以我只是尝试传递函数 do_stuff() ..我知道这行不通,但我不确定要在这里放什么来代替const & functor
范围。我可以传入成员函数吗? - 如何指定类的实例?
我尝试将 lambda 放在那里并且它起作用了 - 但我无法访问 lambda 中的 mp_reader 因为它不在 lambda 的范围内。但无论如何,我真的不想使用 lambda,我真的想使用一个函数(因此,最终我可能能够传入一个外部函数)。
请参见here用于 RTI DDS 功能。这是它所说的关于functor
type:
"Any type whose instances that can be called with a no-argument function call (i.e. f(), if f is an instance of Functor). Examples are functions, types that override the operator(), and lambdas <<C++11>>. The return type has to be void
"