• <i id='ysrp5'><tr id='ysrp5'><dt id='ysrp5'><q id='ysrp5'><span id='ysrp5'><b id='ysrp5'><form id='ysrp5'><ins id='ysrp5'></ins><ul id='ysrp5'></ul><sub id='ysrp5'></sub></form><legend id='ysrp5'></legend><bdo id='ysrp5'><pre id='ysrp5'><center id='ysrp5'></center></pre></bdo></b><th id='ysrp5'></th></span></q></dt></tr></i><div id='ysrp5'><tfoot id='ysrp5'></tfoot><dl id='ysrp5'><fieldset id='ysrp5'></fieldset></dl></div>
  • <legend id='ysrp5'><style id='ysrp5'><dir id='ysrp5'><q id='ysrp5'></q></dir></style></legend>

    <tfoot id='ysrp5'></tfoot>

        • <bdo id='ysrp5'></bdo><ul id='ysrp5'></ul>
      1. <small id='ysrp5'></small><noframes id='ysrp5'>

        如何将成员函数传递给函数指针?

        时间:2023-07-01
        <legend id='lHDfc'><style id='lHDfc'><dir id='lHDfc'><q id='lHDfc'></q></dir></style></legend>

          <small id='lHDfc'></small><noframes id='lHDfc'>

            <bdo id='lHDfc'></bdo><ul id='lHDfc'></ul>
              <tbody id='lHDfc'></tbody>

                  <i id='lHDfc'><tr id='lHDfc'><dt id='lHDfc'><q id='lHDfc'><span id='lHDfc'><b id='lHDfc'><form id='lHDfc'><ins id='lHDfc'></ins><ul id='lHDfc'></ul><sub id='lHDfc'></sub></form><legend id='lHDfc'></legend><bdo id='lHDfc'><pre id='lHDfc'><center id='lHDfc'></center></pre></bdo></b><th id='lHDfc'></th></span></q></dt></tr></i><div id='lHDfc'><tfoot id='lHDfc'></tfoot><dl id='lHDfc'><fieldset id='lHDfc'></fieldset></dl></div>

                  <tfoot id='lHDfc'></tfoot>
                  本文介绍了如何将成员函数传递给函数指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  class Child;
                  class Parent
                  {
                  public:
                    void (*funcPointer)();
                    void (*funcPointer2)(Parent* _this);
                    void (Child::*funcPointer3)();
                  };
                  
                  class Child: public Parent
                  {
                  public:
                    void TestFunc(){
                  
                    }
                    void Do(){
                      Parent p;
                      p.funcPointer=TestFunc; // error, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(void)'
                      p.funcPointer2=TestFunc; // error too, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(Parent *)'
                      p.funcPointer3=TestFunc; //this works
                      p.funcPointer3=&Child::TestFunc; // this works too.
                      p.funcPointer3();    // error, term does not evaluate to a function taking 0 arguments
                    }
                  };
                  

                  如何将成员函数传递给函数指针,然后如何调用该函数指针?

                  How can I pass a member function to a function pointer, and then how would I then call that function pointer?

                  推荐答案

                  你不能.您要么传递一个指向静态方法的指针,要么 Parent 也必须接受一个指向该对象的指针.

                  You can't. You either pass a pointer to a static method or Parent has to accept also a pointer to the object.

                  您可能想查看 boost::bind 和 boost::function:

                  You might want to look at boost::bind and boost::function for that:

                  #include <boost/bind.hpp>
                  #include <boost/function.hpp>
                  struct Y
                  {
                      void say(void) { std::cout << "hallo!";}
                  
                      boost::function<void()> getFunc() { return boost::bind(&Y::say, this); }
                  };
                  
                  struct X
                  {
                      //non-boost:
                      void (Y::*func)();
                      Y* objectY;
                      void callFunc() { (objectY->*func)(); }
                  
                      //boost version:
                      boost::function<void()> f;
                  
                  };
                  
                  
                  X x;
                  Y y;
                  x.f = boost::bind(&Y::say, boost::ref(y));
                  x.f = y.getFunc();
                  x.f();
                  x.func = &Y::say;
                  x.objectY = &y; 
                  x.callFunc();
                  

                  这篇关于如何将成员函数传递给函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么编译这个 C++ 代码片段(非 void 函数不返回值) 下一篇::: 在 C++ 中是什么意思?

                  相关文章

                • <legend id='dT3mo'><style id='dT3mo'><dir id='dT3mo'><q id='dT3mo'></q></dir></style></legend>
                  1. <small id='dT3mo'></small><noframes id='dT3mo'>

                      <bdo id='dT3mo'></bdo><ul id='dT3mo'></ul>

                    1. <tfoot id='dT3mo'></tfoot>
                    2. <i id='dT3mo'><tr id='dT3mo'><dt id='dT3mo'><q id='dT3mo'><span id='dT3mo'><b id='dT3mo'><form id='dT3mo'><ins id='dT3mo'></ins><ul id='dT3mo'></ul><sub id='dT3mo'></sub></form><legend id='dT3mo'></legend><bdo id='dT3mo'><pre id='dT3mo'><center id='dT3mo'></center></pre></bdo></b><th id='dT3mo'></th></span></q></dt></tr></i><div id='dT3mo'><tfoot id='dT3mo'></tfoot><dl id='dT3mo'><fieldset id='dT3mo'></fieldset></dl></div>