使用std::function包裹非静态成员函数指针
Use std::function to wrap non-static member function pointer
我最近声明了与此类似的 class:
class Foo {
public:
void run();
private:
void foo();
void boo();
void doo();
std::function<void()>getFunction(int);
};
在此示例中,我想根据传递的整数获取指向成员函数的指针。
void Foo::run(){
std::function<void()> f;
for(int i = 0; i < 3; i++){
f = getFunction(i);
f();
}
}
std::function<void()>Foo::getFunction(int i){
switch(i){
case 0: return foo;
case 1: return Foo::boo;
case 2: return this->*doo;
}
}
所有情况都会导致编译器错误。将 static
添加到 case 1
的函数中是可行的,但我不喜欢使用静态成员。
有没有什么方法可以在不使用 static
关键字的情况下正确获取这些指针?
您需要绑定一个对象来调用非静态成员函数,在本例中是this
。您可以使用 std::bind
、
std::function<void()> Foo::getFunction(int i) {
switch(i){
case 0: return std::bind(&Foo::foo, *this); // or std::bind(&Foo::foo, this)
case 1: return std::bind(&Foo::boo, *this); // or std::bind(&Foo::boo, this)
case 2: return std::bind(&Foo::doo, *this); // or std::bind(&Foo::doo, this)
}
}
或将std::function<void()>
改为std::function<void(Foo&)>
,以匹配非静态成员函数。那么
void Foo::run() {
std::function<void(Foo&)> f;
for(int i = 0; i < 3; i++) {
f = getFunction(i);
f(*this);
}
}
std::function<void(Foo&)> Foo::getFunction(int i) {
switch(i){
case 0: return &Foo::foo;
case 1: return &Foo::boo;
case 2: return &Foo::doo;
}
}
作为宋元耀回答的延伸
使用 lambda 怎么样? (假设重要的只是能够调用内部函数而不是函数指针本身)
void Foo::run(){
std::function<void()> f;
for(int i = 0; i < 3; i++){
f = getFunction(i);
f();
}
}
std::function<void()> Foo::getFunction(int i) {
switch(i){
case 0: return [this](){this->foo();};
case 1: return [this](){this->boo();};
case 2: return [this](){this->doo();};
}
}
我最近声明了与此类似的 class:
class Foo {
public:
void run();
private:
void foo();
void boo();
void doo();
std::function<void()>getFunction(int);
};
在此示例中,我想根据传递的整数获取指向成员函数的指针。
void Foo::run(){
std::function<void()> f;
for(int i = 0; i < 3; i++){
f = getFunction(i);
f();
}
}
std::function<void()>Foo::getFunction(int i){
switch(i){
case 0: return foo;
case 1: return Foo::boo;
case 2: return this->*doo;
}
}
所有情况都会导致编译器错误。将 static
添加到 case 1
的函数中是可行的,但我不喜欢使用静态成员。
有没有什么方法可以在不使用 static
关键字的情况下正确获取这些指针?
您需要绑定一个对象来调用非静态成员函数,在本例中是this
。您可以使用 std::bind
、
std::function<void()> Foo::getFunction(int i) {
switch(i){
case 0: return std::bind(&Foo::foo, *this); // or std::bind(&Foo::foo, this)
case 1: return std::bind(&Foo::boo, *this); // or std::bind(&Foo::boo, this)
case 2: return std::bind(&Foo::doo, *this); // or std::bind(&Foo::doo, this)
}
}
或将std::function<void()>
改为std::function<void(Foo&)>
,以匹配非静态成员函数。那么
void Foo::run() {
std::function<void(Foo&)> f;
for(int i = 0; i < 3; i++) {
f = getFunction(i);
f(*this);
}
}
std::function<void(Foo&)> Foo::getFunction(int i) {
switch(i){
case 0: return &Foo::foo;
case 1: return &Foo::boo;
case 2: return &Foo::doo;
}
}
作为宋元耀回答的延伸
使用 lambda 怎么样? (假设重要的只是能够调用内部函数而不是函数指针本身)
void Foo::run(){
std::function<void()> f;
for(int i = 0; i < 3; i++){
f = getFunction(i);
f();
}
}
std::function<void()> Foo::getFunction(int i) {
switch(i){
case 0: return [this](){this->foo();};
case 1: return [this](){this->boo();};
case 2: return [this](){this->doo();};
}
}