类构造函数中的静态函数指针

当我在myclass_2的void load(void)方法的构造函数参数中使用非静态函数指针时,出现以下编译器错误。

必须调用对非静态成员函数的引用

如果我将函数更改为static void load(void),则不会出现错误。 但我需要有非静态函数,我必须在加载函数中处理一些全局变量。 任何帮助将不胜感激。

class myclass{
public:
    //_load_event_handler
    vector<Function> _handler_one;
    vector<Function> _handler_two;
    vector<Function> _handler_three;

    void register_for_load_event(void(*fun)(void)){
        this->_handler_three.push_back(fun);
    }

    void register_for_dowork_event(void(*fun)(void)){
        this->_handler_one.push_back(fun);
    }
    void register_for_end_event(void(*fun)(void)){
        this->_handler_two.push_back(fun);
    }
    void run (char **fileNames)
    {
        for (int i = 0; i< _handler_one.size();i++)
            _handler_one[i]();


        for (int i = 0; i< _handler_two.size();i++)
            _handler_two[i]();


        for (int i = 0; i< _handler_three.size();i++)
            _handler_three[i]();
    }


};

class myclass_2
{
public:
    myclass *ptrmyclass;
    void load(void)
    {
        cout << "load function start" << endl;

        //some code here
    }
    myclass_2(myclass *_ptrmyclass)
    {
        ptrmyclass = _ptrmyclass;
        ptrmyclass->register_for_load_event(load);

    }
    void foo(vector<string> vect)
    {
        cout<< "is stop word called" << endl;
    }
};

这是你需要的吗?

修改:

  • 我没有看到问题中的Function是什么,所以我认为这是std::function<void()>
  • register_for_load_event输入应该是一个有效的函数对象,所以它需要一个bind

  • using namespace std;
    class myclass{
    public:
        //_load_event_handler
        typedef std::function<void()> Function; // -------- #1
        vector<Function> _handler_one;
        vector<Function> _handler_two;
        vector<Function> _handler_three;
    
        void register_for_load_event(Function fun){
            this->_handler_three.push_back(fun);
        }
    
        void register_for_dowork_event(Function fun){
            this->_handler_one.push_back(fun);
        }
        void register_for_end_event(Function fun){
            this->_handler_two.push_back(fun);
        }
        void run (char **fileNames)
        {
            for (int i = 0; i< _handler_one.size();i++)
                _handler_one[i]();
    
    
            for (int i = 0; i< _handler_two.size();i++)
                _handler_two[i]();
    
    
            for (int i = 0; i< _handler_three.size();i++)
                _handler_three[i]();
        }
    
    
    };
    
    class myclass_2
    {
    public:
        myclass *ptrmyclass;
        void load(void)
        {
            cout << "load function start" << endl;
    
            //some code here
        }
        myclass_2(myclass *_ptrmyclass)
        {
            ptrmyclass = _ptrmyclass;
            ptrmyclass->register_for_load_event(bind(&myclass_2::load, this)); // -------- #2
    
        }
        void foo(vector<string> vect)
        {
            cout<< "is stop word called" << endl;
        }
    };
    

    确保Functionstd::function<void()> ,然后更改register_for_load_event以获取Function而不是现在需要的Function 。 最后,这样称呼它:

    ptrmyclass->register_for_load_event(std::bind(&myclass_2::load, this));
    

    因为Function是nullary(不带参数),所以需要std::bind() ,但myclass_2::load()需要一个参数:隐式this (因为它是非静态的,所以需要myclass_2一个实例“Binding”成员函数允许你在绑定时提供this指针,但是延迟调用这个函数直到以后,当不需要参数。

    链接地址: http://www.djcxy.com/p/96381.html

    上一篇: static function pointer in a class constructor

    下一篇: static member function as pointer to a function?