传递和调用成员函数(boost :: bind / boost :: function?) [英] Pass and call a member function (boost::bind / boost::function?)
问题描述
我可能有一个很尴尬的简单问题:在类中传递和调用成员函数。我知道我想使用BOOST绑定(和或函数),但我还没有真正把握它的概念。
下面的代码编译和执行与问题。但是当我想将f3函数改为非静态类函数时,有趣的开始:
#include < iostream>
#include< inttypes.h>
#include< boost / bind.hpp>
#include< boost / function.hpp>
class Test
{
public:
void f1();
private:
void f2(void(* callfunc)(uint32_t));
static void f3(uint32_t x);
};
void Test :: f1(){
f2(f3);
}
void Test :: f2(void(* callfunc)(uint32_t)){
(* callfunc)(42);
}
void Test :: f3(uint32_t x){
std :: cout< x:<< x<< std :: endl;
}
int main(int argc,char ** argv)
{
Test ct;
ct.f1();
return 0;
}
现在,更改后
static void f3(uint32_t x);
到
void f3(uint32_t x);
编译器不开心,告诉我错误:没有匹配函数调用' :f2()'
通过阅读了很多关于boost :: bind和boost :: function的SO文章,我想我需要改变f2的定义()和如何f1()调用f2()给f3()作为目标调用,但除此之外...关于boost :: bind和boost函数的每一个组合我试图惨败编译失败。
我该如何写这篇文章?作为一个额外的问题:是否有任何简单的介绍读取boost :: bind和boost :: function?
p> boost :: function是一个模板类,它接受一个函数签名。您也可以使用function0,function1等。
boost :: function< void(uint32_t)>
定义一个看起来像一个函数的可调用,即它需要一个类型 uint32_t
并返回void。
相应的编号模板是 function1< void,uint32_t>
。这些总是首先指示返回类型,然后按顺序显示参数。
boost :: bind
函数,它推导你传递给它的参数,并为你创建一个函数。
它不会为你创建一个void(uint32_t)
因此将您的签名更改为:
f2(boost :: function< void(uint32_t)>);
然后你可以这样调用:
f2(boost :: bind(& Test :: f3,this,_1));
请注意,奇怪的_1是一个占位符告诉boost :: bind它需要放在参数,在这种情况下为 uint32_t
I have a probably embarassingly simple problem: pass and call a member function in a class. I know I want to use BOOST bind (and or function), but I haven't really grasped the concept to it yet.
The following code compiles and executes with problem. But when I want to change the "f3" function to a non-static class function, then the fun begins:
#include <iostream>
#include <inttypes.h>
#include <boost/bind.hpp>
#include <boost/function.hpp>
class Test
{
public:
void f1();
private:
void f2(void (*callfunc)(uint32_t));
static void f3(uint32_t x);
};
void Test::f1(){
f2(f3);
}
void Test::f2(void (*callfunc)(uint32_t)){
(*callfunc)(42);
}
void Test::f3(uint32_t x){
std::cout << "x: " << x << std::endl;
}
int main(int argc, char ** argv)
{
Test ct;
ct.f1();
return 0;
}
Now, after changing
static void f3(uint32_t x);
to
void f3(uint32_t x);
the compiler isn't happy and tells me "error: no matching function for call to 'Test::f2()'"
Having read through a number of SO posts regarding boost::bind and boost::function, I think I need to change the definition of f2() and how f1() calls f2() giving f3() as target to call, but apart from that ... about every combination of boost::bind and boost function I tried miserably fails to compile.
How do I need to write this? As a bonus question: are there any simple introductory reads on boost::bind and boost::function? The BOOST docs did not really help me there.
B.
boost::function is a template class, that takes a function signature. You can also use function0, function1, etc.
boost::function< void(uint32_t) >
defines a "callable" that looks like a function, i.e. it takes a single parameter of type uint32_t
and returns void.
The appropriate numbered template is function1< void, uint32_t >
. These always indicate the return type first, then the parameters in order.
boost::bind
is a very special function that deduces the arguments you pass into it and creates a functor for you.
It will not create a void(uint32_t) for you, it will create something that has the pattern of one.
Therefore change your signature to:
void f2(boost::function<void(uint32_t)>);
Then you can call it like this:
f2( boost::bind( &Test::f3, this, _1 ) );
Note the strange _1 is a "placeholder" telling boost::bind where it needs to put in the parameter, in this case the uint32_t
这篇关于传递和调用成员函数(boost :: bind / boost :: function?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!