伊莉討論區

標題: 請問 c++ callback function問題 [打印本頁]

作者: dhhuang618    時間: 2020-4-4 05:50 PM     標題: 請問 c++ callback function問題

各位大大,

小弟有個 C++問題
請問大家要如何用 c++的 class作出 callback function ?

謝謝各位

作者: baepi    時間: 2020-4-7 12:12 PM

callback function ?
真抱歉我不是資工資管畢業的...所以按照我以為的以為來給予答案...倘若不是你期望的答案....那就只好再請其他高人解答了
一般來說...對於涵式呼叫涵式的做法(以上這句腦殘的翻譯是我自我的理解)...我常用的有以下兩種...我都塞到同一個class裡面了....
  1. class QQ
  2. {
  3. public:
  4.         int(*_int_int_fun)(int);
  5.         void(*_void_fun)();
  6.         QQ();
  7.         void test_1(int _int = 0);
  8.         void test_2(void(*_void_point)() = NULL , int(*_int_int_point)(int _int) = NULL, int _int = 0);
  9. };
  10. QQ::QQ()
  11. {
  12.         _int_int_fun = NULL;
  13.         _void_fun = NULL;
  14. }
  15. void QQ::test_1(int _int)
  16. {
  17.         if (_int_int_fun != NULL)
  18.         {
  19.                 cout << _int_int_fun(_int) << endl;;
  20.         }
  21.         if (_void_fun != NULL)
  22.         {
  23.                 _void_fun();
  24.         }
  25. }
  26. void QQ::test_2(void(*_void_point)() , int(*_int_int_point)(int _int) , int _int)
  27. {
  28.         if (_int_int_point != NULL)
  29.         {
  30.                 cout << _int_int_point(_int) << endl;;
  31.         }
  32.         if (_void_point != NULL)
  33.         {
  34.                 _void_point();
  35.         }
  36. }

  37. void aa()
  38. {
  39.         cout << "A" << endl;
  40. }
  41. int cc(int c)
  42. {
  43.         cout << c << endl;
  44.         return c + 1;
  45. }
  46. void main()
  47. {
  48.         QQ q1;
  49.         q1._int_int_fun = cc;
  50.         q1._void_fun = aa;
  51.         q1.test_1();
  52.         q1.test_2(aa , cc , 2);
  53.         system("pause");
  54. }
複製代碼

作者: kiwis    時間: 2020-4-7 10:27 PM

本帖最後由 kiwis 於 2020-4-7 10:54 PM 編輯

>如何用 c++的 class作出 callback function ?
你是指把 C++ 的 class 拿來當成 callback function 用嗎?
有是有啦,不過個人覺得蠻鳥的,就是用[使用者定義轉換運算子]這招,
告訴編譯器這個 class 是個 callback function就行了。
  1. //CallerClass.h
  2. #ifndef CALLERCLASS
  3. #define CALLERCLASS

  4. #include <stdio.h>

  5. // typedef 一下 callback function 長甚麼樣,等下可以省打幾個字
  6. typedef void (*mycallback) (int);

  7. class CallerClass
  8. {
  9. public:
  10.         CallerClass();
  11.         virtual ~CallerClass();

  12.         // 設定 mycallback
  13.         void SetCallback(mycallback new_func);

  14.         void SomeFunction(int num);

  15. private:

  16.         mycallback callback;

  17. };

  18. #endif
複製代碼
  1. //CallerClass.cpp
  2. #include "CallerClass.h"

  3. CallerClass::CallerClass():
  4.   callback(NULL)
  5. {
  6. }

  7. CallerClass::~CallerClass()
  8. {
  9.         p rintf("CallerClass destrucutor called.\n");
  10. }

  11. void CallerClass::SomeFunction(int num)
  12. {
  13.         p rintf("SomeFunction() called.\n");

  14.         if(callback)
  15.                 callback(num);
  16.         else
  17.                 p rintf("callback function is not set.\n");


  18.         p rintf("SomeFunction() exiting.\n\n");
  19. }

  20. void CallerClass::SetCallback(mycallback new_func)
  21. {
  22.         callback = new_func;
  23. }
複製代碼
  1. //FunctionImpl.h
  2. #ifndef FUNCIMPL
  3. #define FUNCIMPL

  4. #include <stdio.h>

  5. typedef void (*mycallback) (int);

  6. class FunctionImpl
  7. {
  8. public:

  9.         // 一般的 callback function
  10.         static void FunctionOne(int num);
  11.         static void FunctionTwo(int num);

  12.         // 讓 FunctionImpl class 被認成 mycallback
  13.         operator mycallback() const;

  14. private:
  15.         // 實際上被 operator mycallback() const 傳回去的是這個 private function
  16.         static void FunctionPrivate(int num);
  17. };
  18. #endif
複製代碼
  1. //FunctionImpl.cpp
  2. #include "FunctionImpl.h"

  3. void FunctionImpl::FunctionOne(int num)
  4. {
  5.         p rintf("FunctionOne: %d\n", num);
  6. }

  7. void FunctionImpl::FunctionTwo(int num)
  8. {
  9.         p rintf("FunctionTwo: %d\n", num+num);
  10. }

  11. void FunctionImpl::FunctionPrivate(int num)
  12. {
  13.         p rintf("FunctionPrivate: %d\n", num*99);
  14. }

  15. FunctionImpl::operator mycallback() const
  16. {
  17.         // 傳回 FunctionPrivate 的位置
  18.         return FunctionPrivate;
  19. }
複製代碼
實際操作:
  1. #include "FunctionImpl.h"
  2. #include "CallerClass.h"


  3. int main(int argc, char* argv[])
  4. {
  5.         CallerClass caller;
  6.         FunctionImpl funcs;

  7.         // 未設定 callback 的狀態:
  8.         caller.SomeFunction(1);

  9.         // 設定 funcs.FunctionOne 為 callback
  10.         caller.SetCallback(funcs.FunctionOne);
  11.         caller.SomeFunction(1);

  12.         // 設定 funcs.FunctionTwo 為 callback
  13.         caller.SetCallback(funcs.FunctionTwo);
  14.         caller.SomeFunction(1);

  15.         // 直接把 funcs (FunctionImpl class) 當成 callback 直接給他設下去
  16.         caller.SetCallback(funcs);
  17.         caller.SomeFunction(1);

  18.         // 清空 callback
  19.         caller.SetCallback(NULL);
  20.         caller.SomeFunction(1);

  21.         return 0;
  22. }
複製代碼
執行結果:
SomeFunction() called.
callback function is not set.
SomeFunction() exiting.

SomeFunction() called.
FunctionOne: 1
SomeFunction() exiting.

SomeFunction() called.
FunctionTwo: 2
SomeFunction() exiting.

SomeFunction() called.
FunctionPrivate: 99
SomeFunction() exiting.

SomeFunction() called.
callback function is not set.
SomeFunction() exiting.

CallerClass destrucutor called.

作者: cockroachrun    時間: 2020-4-8 09:29 AM

老實說
樓主問的問題是甚麼? 我都搞不懂.
是要把class 的member function 傳給系統API 如 _beginthreadex(... ) 使用
還是如2,3 樓說的功能?







歡迎光臨 伊莉討論區 (http://www033.eyny.com/) Powered by Discuz!