标签: member-function-pointers

如何使用指向成员函数的指针进行切换?

好吧,我想要做的只是一个带有函数指针的“开关”,但带有方法指针。开关是,如果我调用方法Run(),它将重定向到A::RunOn()A::RunOff()根据Runptr 指向这些成员函数。

我知道这是可以做到的。我是用普通的但我已经搜索并用 google 搜索了在做同样的事情,但没有运气。

class A
{
    typedef (void)(A::*RunPtr)(int);
    RunPtr RunMethod;

public:
    RunMethod Run;

    A()
    {
        Run = RunOff;
    }

    void SetOn(bool value)
    {
        if (value)
            Run = RunOn;
        else
            Run = RunOff;
    }

    void RunOn(int)
    {
        // RunOn stuff here
    }

    void RunOff(int)
    {
        // RunOff stuff here
    }
};
Run Code Online (Sandbox Code Playgroud)

所以我可以调用,Run()并且在函数调用之间会有一个切换,我认为这比仅仅这样做更有效:

if (on)
    RunOn();
else
    RunOff();
Run Code Online (Sandbox Code Playgroud)

不知道怎么办!

c++ algorithm member-function-pointers class member-functions

3
推荐指数
2
解决办法
154
查看次数

重新解释成员函数指针是个"好主意"吗?

我有一个工作线程,它包含一个"线程操作"列表,并作为一个时间通过它们.

template <class T> class ThreadAction
{
public:

  typedef void (T::*action)();

  ThreadAction(T* t, action f) :
    func(f),obj(t) {}
  void operator()() { (obj->*func)(); }

  void (T::*func)();
  T* obj;

};
Run Code Online (Sandbox Code Playgroud)

它通常被称为这样

myActionThread->addAction(
    new ThreadAction<TheirClass>(this, &TheirClass::enable)
);
Run Code Online (Sandbox Code Playgroud)

哪个工作正常,直到

 void TheirClass::enable()
Run Code Online (Sandbox Code Playgroud)

被改为

 bool TheirClass::enable()
Run Code Online (Sandbox Code Playgroud)

遗憾的是,我们无法再将其更改回来,因为其他内容需要新的格式(并且过载因单独的返回类型而无法区分).

我确实试过了

myActionThread->addAction( 
    new ThreadAction<TheirClass>(this, 
        reinterpret_cast<void(TheirClass::*)>(&TheirClass::enable)
    )
);
Run Code Online (Sandbox Code Playgroud)

哪个似乎工作正常,但我不确定重新解释像这样的函数指针是'定义'行为,有人可以建议吗?

c++ member-function-pointers reinterpret-cast

2
推荐指数
1
解决办法
247
查看次数

成员函数的函数指针

有几个副本,但没有人解释为什么我可以使用成员变量来存储指针(in FOO)但是当我尝试使用局部变量(在注释部分BAR)时,它是非法的.有人可以解释一下吗?

#include <iostream>
using namespace std;

class FOO
{
public:
 int (FOO::*fptr)(int a, int b);
 int add_stuff(int a, int b)
 {
  return a+b;
 }
 void call_adder(int a, int b)
 {  
  fptr = &FOO::add_stuff;
  cout<<(this->*fptr)(a,b)<<endl;
 }
};

class BAR
{
public:
 int add_stuff(int a, int b)
 {
  return a+b;
 }
 void call_adder(int a, int b)
 {  
  //int (BAR::*fptr)(int a, int b);
  //fptr = &BAR::add_stuff;
  //cout<<(*fptr)(a,b)<<endl;
 }
};

int main()
{
 FOO test;
 test.call_adder(10,20);
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ member-function-pointers class

2
推荐指数
2
解决办法
4764
查看次数

c ++泛型指针(member?)函数

我似乎无法声明一个函数的泛型指针.

要调用这两个函数:

void myfunc1(std::string str)
{
    std::cout << str << std::endl;
}
struct X
{
        void f(std::string str){ std::cout<< str << std::endl;}
};
Run Code Online (Sandbox Code Playgroud)

和这两个函数调用者:

typedef void (*userhandler_t) (std::string);
struct example
{   
    userhandler_t userhandler_;

    example(userhandler_t userhandler): userhandler_(userhandler){}

    void call(std::string str)
    {   
        userhandler_(str);
    }
};
template<typename func_t>
void justfunc(func_t func)
{
    func("hello, works!");
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用它们与boost :: bind来调用成员函数时,它们会给我编译错误.

这工作:

example e1(&myfunc1);
e1.call("hello, world!");
justfunc(&myfunc1);
Run Code Online (Sandbox Code Playgroud)

这不是:

X x;
example e2(boost::bind(&X::f, &x, _1));
e2.call("hello, world2!");
justfunc(boost::bind(&X::f, &x, _1));
Run Code Online (Sandbox Code Playgroud)

应该怎么做?

c++ member-function-pointers function-pointers boost-bind

2
推荐指数
1
解决办法
1086
查看次数

指向成员函数的指针问题

在下面的代码中(请参阅评论):

#include "stdafx.h"

#include <iostream>
using std::cout;

struct Base
{
 void fnc()
 {
  cout << "Base::fnc()";
 }

};

struct Impl
{
 void* data_;
 Impl(void (Base::*fp)())
 {
  fp();//HERE I'M INVOKING IT - I'M DOING SOMETHING WRONG!
 }
};
int _tmain(int argc, _TCHAR* argv[])
{
 return 0;
}  
Run Code Online (Sandbox Code Playgroud)

错误
"错误1错误C2064:术语不评估为采用0参数的函数"为什么它不起作用以及如何解决它?

c++ member-function-pointers

2
推荐指数
1
解决办法
1063
查看次数

如何使用指向成员函数的函数调用函数

我有一节课:

class A {
    void test_func_0(int);
    void run();

    typedef void(A::*test_func_t)(int);

    struct test_case_t{
       test_func_t test_func;
    } test_case[100];
};
Run Code Online (Sandbox Code Playgroud)

现在我想在run()中调用test_func():

void A::run() 
{
    test_case[0].test_func = &test_func_0;
    test_case[0].*(test_func)(1);
}
Run Code Online (Sandbox Code Playgroud)

无论我尝试什么组合,我的代码的最后一行都不起作用(编译错误).

c++ member-function-pointers function-pointers function

2
推荐指数
1
解决办法
427
查看次数

使用libevent回调成员函数

我试图将一个成员函数传递给libevent,它应被视为回调.

#include <event.h>

class A
{
    public:
        void eventcb(evutil_socket_t fd, short events, void *ctx) { }
};


static void global_eventcb(evutil_socket_t fd, short events, void *ctx) { }

typedef void (A::*mthd)(evutil_socket_t, short, void*);

int main(void)
{
    struct event_base *evbase = event_base_new();

    mthd eventcb = &A::eventcb;
    A *instance = new A;
    (instance->*eventcb)(NULL, 0, NULL);

    struct event *timer1 = evtimer_new(evbase, global_eventcb, NULL);
    struct event *timer2 = evtimer_new(evbase, (instance->*eventcb), NULL);

    return 0;
}   
Run Code Online (Sandbox Code Playgroud)

我可以在A类中成功创建一个指向eventcb的方法指针,在A的实例上调用它(第20行).

此外,在第22行传递全局函数(如在C中一样)也可以正常工作.

但是,在第23行,我尝试将我的方法指针传递给libevent,当我编译它时,我得到以下错误(使用 …

c++ member-function-pointers libevent

2
推荐指数
1
解决办法
4629
查看次数

无法将类成员函数传递给另一个函数(std :: thread :: thread)

看看这两个代码.

下面的代码工作正常.

void someFunction () {

    // Some unimportant stuff
}

MainM::MainM(QObject *parent) :
    QObject(parent)
{

    std::thread oUpdate (someFunction);

}
Run Code Online (Sandbox Code Playgroud)

此代码抛出错误:

void MainM::someFunction () {      //as a class member


}


MainM::MainM(QObject *parent) :
    QObject(parent)
{

    std::thread oUpdate (someFunction);

}
Run Code Online (Sandbox Code Playgroud)

错误:

error: no matching function for call to 'std::thread::thread(<unresolved overloaded function type>)'
     std::thread oUpdate (someFunction);
                                     ^
Run Code Online (Sandbox Code Playgroud)

c++ member-function-pointers c++11 stdthread

2
推荐指数
1
解决办法
799
查看次数

将成员函数传递给另一个对象的成员函数C++

我在尝试将函数作为参数传递给另一个对象的函数时遇到问题.我很清楚有很多类似的主题,但我要么无法让他们的解决方案工作或无法理解它们.

class foo
{
public:
    void func1(void (*Drawing)(void));

    template<class T>
    void func2(void (T::*Drawing)(void));
};

class bar
{
private:
    foo myFoo;
    void Drawing();
    void func3() {
        // Attempt 1
        myFoo.func1(Drawing);

        // Attempt 2
        myFoo.func2<bar>(&bar::Drawing);
    }
};
Run Code Online (Sandbox Code Playgroud)

所以在我的第一次尝试中,我得到的错误是你无法转换void (bar::*)(void)void (*)(void)我发现有正常的函数指针和成员函数指针.

尝试2是我努力克服这个问题,但我现在得到了未解决的外部因素......

那么如何成功将我的Drawing()成员函数从另一个对象传递到另一个函数呢?

c++ member-function-pointers function-pointers member-functions

2
推荐指数
1
解决办法
801
查看次数

当前类的成员函数上的指针数组

我尝试创建一个当前类的成员函数指针数组,但暂时没有成功...

我已经尝试了很多东西,这是我的代码:

// Human.hpp

#include <iostream>

class Human
{
    private:
        void meleeAttack(std::string const & target);
        void rangedAttack(std::string const & target);
        void intimidatingShout(std::string const & target);
    public:
        void action(std::string const & action_name, std::string const & target);
};
Run Code Online (Sandbox Code Playgroud)
// Human.cpp

#include "Human.hpp"

// ...

void    Human::action(std::string const & action_name, std::string const & target)
{
    std::string actionsStr[] = {"meleeAttack", "rangedAttack", "intimidatingShout"};

    typedef void (Human::*Actions)(std::string const & target);
    Actions actions[3] = {&Human::meleeAttack, &Human::rangedAttack, &Human::intimidatingShout};

    for (int i = 2; i >= 0; i--)
        if …
Run Code Online (Sandbox Code Playgroud)

c++ arrays member-function-pointers function-pointers

2
推荐指数
1
解决办法
1638
查看次数