• <tfoot id='7qbJr'></tfoot>
    <legend id='7qbJr'><style id='7qbJr'><dir id='7qbJr'><q id='7qbJr'></q></dir></style></legend>

    <small id='7qbJr'></small><noframes id='7qbJr'>

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

        • <bdo id='7qbJr'></bdo><ul id='7qbJr'></ul>

        C++ 函数回调:无法从成员函数转换为函数签名

        时间:2023-06-29
          <tbody id='t1r6A'></tbody>
        <i id='t1r6A'><tr id='t1r6A'><dt id='t1r6A'><q id='t1r6A'><span id='t1r6A'><b id='t1r6A'><form id='t1r6A'><ins id='t1r6A'></ins><ul id='t1r6A'></ul><sub id='t1r6A'></sub></form><legend id='t1r6A'></legend><bdo id='t1r6A'><pre id='t1r6A'><center id='t1r6A'></center></pre></bdo></b><th id='t1r6A'></th></span></q></dt></tr></i><div id='t1r6A'><tfoot id='t1r6A'></tfoot><dl id='t1r6A'><fieldset id='t1r6A'></fieldset></dl></div>
          • <bdo id='t1r6A'></bdo><ul id='t1r6A'></ul>

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

                <tfoot id='t1r6A'></tfoot>

                1. <legend id='t1r6A'><style id='t1r6A'><dir id='t1r6A'><q id='t1r6A'></q></dir></style></legend>
                  本文介绍了C++ 函数回调:无法从成员函数转换为函数签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用第 3 方库,它允许我为某些事件注册回调.register 函数看起来像这样.它使用回调签名.

                  I'm using a 3rd party library that allows me to register callbacks for certain events. The register function looks something like this. It uses the Callback signature.

                  typedef int (*Callback)(std::string);
                  
                  void registerCallback(Callback pCallback) {
                  //it gets registered
                  }
                  

                  我的问题是我想注册一个成员函数作为这样的回调

                  My problem is that I want to register a member function as a callback something like this

                  struct MyStruct {
                      MyStruct();
                      int myCallback(std::string str);
                  };
                  
                  MyStruct::MyStruct() {
                      registerCallback(&MyStruct::myCallback);
                  }
                  
                  int MyStruct::myCallback(std::string str) {
                      return 0;
                  }
                  

                  当然,编译器会抱怨,说

                  Of course, the compiler complains, saying

                  错误 C2664:registerCallback":无法将参数 1 从int (__thiscall MyStruct::*)(std::string)"转换为Callback"

                  error C2664: 'registerCallback' : cannot convert parameter 1 from 'int (__thiscall MyStruct::* )(std::string)' to 'Callback'

                  我一直在寻找像 function 和 bind 这样的 boost 库,但似乎没有一个能够做到这一点.我一直在 Google 上到处搜索答案,但我什至不知道该怎么称呼它,所以没有太大帮助.

                  I've been looking at boost libraries like function and bind, but none of those seem to be able to do the trick. I've been searching all over Google for the answer, but I don't even know what to call this, so it hasn't been much help.

                  提前致谢.

                  推荐答案

                  您正试图将成员函数指针作为普通函数指针传递,但该指针不起作用.成员函数必须将this指针作为隐藏参数之一,普通函数不是这样,因此它们的类型不兼容.

                  You're trying to pass a member function pointer as a normal function pointer which won't work. Member functions have to have the this pointer as one of the hidden parameters, which isn't the case for normal functions, so their types are incompatible.

                  您可以:

                  1. 更改参数类型以接受成员函数并接受一个实例作为调用对象
                  2. 不再尝试传递成员函数并传递普通函数(可能通过使函数static)
                  3. 有一个普通函数,它接受你的类的一个实例、一个成员函数指针和一个 std::string 并使用类似 boost 的 bind 的东西来绑定第一个两个参数
                  4. 让回调注册函数接受一个函子对象,或者一个std::function(我认为这就是名字)
                  5. 我不会在此详述的许多其他方式,但您会有所了解.
                  1. Change the type of your argument to accept member functions and also accept an instance to be the invoking object
                  2. Quit trying to pass a member function and pass a normal function (perhaps by making the function static)
                  3. Have a normal function that takes an instance of your class, a member function pointer, and a std::string and use something like boost's bind to bind the first two arguments
                  4. Make the callback registration function accept a functor object, or an std::function (I think that's the name)
                  5. Numerous other ways which I won't detail here, but you get the drift.

                  这篇关于C++ 函数回调:无法从成员函数转换为函数签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:提升 zip_iterator 和 std::sort 下一篇:使用 boost 线程和非静态类函数

                  相关文章

                2. <small id='VK2Rc'></small><noframes id='VK2Rc'>

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

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