English 中文(简体)
背信非静态 C++ 成员职能
原标题:Callback to a non-static C++ Member Function

我要向大家介绍一下这部令我困惑的那部法典。

   //-------------------------------------------------------------------------------
   // 3.5 Example B: Callback to member function using a global variable
   // Task: The function  DoItB  does something that implies a callback to
   //       the member function  Display . Therefore the wrapper-function
   //        Wrapper_To_Call_Display is used.

   #include <iostream.h>   // due to:   cout

   void* pt2Object;        // global variable which points to an arbitrary object

   class TClassB
   {
   public:

      void Display(const char* text) { cout << text << endl; };
      static void Wrapper_To_Call_Display(char* text);

      /* more of TClassB */
   };


   // static wrapper-function to be able to callback the member function Display()
   void TClassB::Wrapper_To_Call_Display(char* string)
   {
       // explicitly cast global variable <pt2Object> to a pointer to TClassB
       // warning: <pt2Object> MUST point to an appropriate object!
       TClassB* mySelf = (TClassB*) pt2Object;

       // call member
       mySelf->Display(string);
   }


   // function does something that implies a callback
   // note: of course this function can also be a member function
   void DoItB(void (*pt2Function)(char* text))
   {
      /* do something */

      pt2Function("hi, i m calling back using a global ;-)");   // make callback
   }


   // execute example code
   void Callback_Using_Global()
   {
      // 1. instantiate object of TClassB
      TClassB objB;


      // 2. assign global variable which is used in the static wrapper function
      // important: never forget to do this!!
      pt2Object = (void*) &objB;


      // 3. call  DoItB  for <objB>
      DoItB(TClassB::Wrapper_To_Call_Display);
   }

问题1: 关于这项职能:

DoItB(TClassB::Wrapper_To_Call_Display)

为什么Wrapper_To_Call_Display不提出任何论点,尽管根据声明应当采用char*的论点?

<><> <><><>>>>>>><>>>>>>>

void DoItB(void (*pt2Function)(char* text))

我迄今所理解的是:<代码>。 DoItB 以职能要点为理由,但为何职能称为<代码>。 DoItB(TClassB):Wrapper_To_Call_Display)。 甚至可以说,它不是指人吗?

预兆

Source of code snippet: http://www.newty.de/fpt/callback.html

最佳回答

在C/C++中,如果使用功能名称时没有参数,即没有括号,那么它就是一个职能点。 因此,TClassB:Wrapper_To_Call_Display是执行职务守则的记忆中点。

TClassB:Wrapper_To_Call_Display>以来,是<0> /code>的一个联络点,其职能是:*。 时间为<代码> 避免(*)(char* 测试),使之与<代码>所要求的类型相符。 DoItB。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签