• <legend id='5AK8N'><style id='5AK8N'><dir id='5AK8N'><q id='5AK8N'></q></dir></style></legend>

        <small id='5AK8N'></small><noframes id='5AK8N'>

        <tfoot id='5AK8N'></tfoot>

          <bdo id='5AK8N'></bdo><ul id='5AK8N'></ul>

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

      2. 如何在不传递类实例的情况下从静态成员函数调用非静态成员函数

        时间:2023-06-30
          <tbody id='G8HlW'></tbody>

            <bdo id='G8HlW'></bdo><ul id='G8HlW'></ul>
          • <tfoot id='G8HlW'></tfoot>

              • <small id='G8HlW'></small><noframes id='G8HlW'>

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

                  本文介绍了如何在不传递类实例的情况下从静态成员函数调用非静态成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要从同一个类的静态成员函数调用一个非静态成员函数.静态函数是一个回调.它只能接收 void 作为数据,尽管我传递了一个 char*.所以我不能直接向回调提供类实例.我可以将结构而不是 char 传递给回调函数.任何人都可以提供例如代码以在静态成员函数中使用非静态成员函数.并使用静态成员函数中的结构体来使用类的实例调用非静态成员函数?

                  I need to call a non static member function from a static member function of the same class. The static function is a callback. It can receive only void as data, though which i pass a char*. So i cannot directly provide the class instance to the callback. I can pass a structure instead of char to the callback function. Can anyone give eg code to use the non static member function in a static member function . and use the structure in the static member function to use the instance of the class to call the non static member function?

                  推荐答案

                  通常这样的回调看起来像这样:

                  Normally such a callback would look like this:

                  void Callback( void* data)
                  {
                      CMyClass *myClassInstance = static_cast<CMyClass *>(data);
                      myClassInstance->MyInstanceMethod();
                  }
                  

                  当然,您需要确保数据指向您的类的实例.例如

                  Of course, you need to make sure, data points to an instance of your class. E.g.

                  CMyClass* data = new CMyClass();
                  FunctionCallingMyCallback( data, &Callback);
                  delete data;
                  

                  现在,如果我理解正确的话,您还需要传递一个字符*.您可以将两者都包装在一个结构中并在回调中将其解包,如下所示:

                  Now, if I understand you correctly, you need to also pass a char*. You can either wrap both in a struct and unwrap it in the callback like so:

                  MyStruct* data = new MyStruct();
                  data->PtrToMyClass = new CMyClass();
                  data->MyCharPtr = "test";
                  FunctionCallingMyCallback( data, &Callback);
                  delete data->PtrToMyClass;
                  delete data;
                  
                  
                  void Callback( void* data)
                  {
                      MyStruct *myStructInstance = static_cast<MyStruct *>(data);
                      CMyClass *myClassInstance = myStructInstance->PtrToMyClass;
                      char * myData = myStructInstance->MyCharPtr;
                      myClassInstance->MyInstanceMethod(myData);
                  }
                  

                  或者,如果您可以修改 CMyClass 的定义,请将所有必要的数据放入类成员中,以便您可以像第一个示例中那样使用回调.

                  or, if you can modify the definition of CMyClass, put all the necessary data in class members, so that you can use a callback as in the first example.

                  这篇关于如何在不传递类实例的情况下从静态成员函数调用非静态成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:注意: 'person::person()' 被隐式删除,因为默认定义格式错误 下一篇:如何通过 Windows Defender SmartScreen 保护?

                  相关文章

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

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