std::bind a static member function inside the class

前端 未结 1 1692
梦如初夏
梦如初夏 2021-01-02 08:42

I am trying to store a function to call later, here is a snippet.

This works fine:

void RandomClass::aFunc( int param1, int param2, double param3, bo         


        
相关标签:
1条回答
  • 2021-01-02 09:03

    The type of a pointer to static member function looks like a pointer to a non-member fuinction:

    auto storeFunc = std::bind ( (void(*)(WORD, WORD, double, bool))
                                  &CSoundRouteHandlerApp::MakeRoute, 
                                  sourcePort, destPort, volume, true );
    

    Here's a simplified example:

    struct Foo
    {
      void foo_nonstatic(int, int) {}
      static int foo_static(int, int, int) { return 42;}
    };
    
    #include <functional>
    int main()
    {
      auto f_nonstatic = std::bind((void(Foo::*)(int, int))&Foo::foo_nonstatic, Foo(), 1, 2);
      auto f_static = std::bind((int(*)(int, int, int))&Foo::foo_static, 1, 2, 3);
    
    }
    
    0 讨论(0)
提交回复
热议问题