找回密碼 或 安全提問
 註冊
|註冊|登錄

伊莉討論區

搜索
尊貴會員無限使用任何功能搞笑、娛樂、精彩的影片讓你看你準備好成為出色的版主了嗎?
催眠催眠vr259luxumgrpg無碼 ge
星有所屬room監獄無碼 ge巫女神さ洋土豪我心里

休閒聊天興趣交流學術文化旅遊交流飲食交流家庭事務PC GAMETV GAME
熱門線上其他線上感情感性寵物交流家族門派動漫交流貼圖分享BL/GL
音樂世界影視娛樂女性頻道潮流資訊BT下載區GB下載區下載分享短片
電腦資訊數碼產品手機交流交易廣場網站事務長篇小說體育運動時事經濟
上班一族博彩娛樂

[簡]為了在異世界也能

余家儀 衣服直接滑下

[繁]香格里拉・開拓異

[繁]反派千金等級99~

[簡]SHAMAN KING FLOW

[繁/無修正]夢想成為
C & C++ 語言C# 語言Visual Basic 語言PHP 語言JAVA 語言
查看: 3730|回復: 3

[問題] 請問 c++ callback function問題[複製鏈接]

帖子
99
積分
-41 點
潛水值
170 米
發表於 2020-4-4 05:50 PM|顯示全部樓層
各位大大,

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

謝謝各位
回覆中加入附件並不會使你增加積分,請使用主題方式發佈附件。

使用道具檢舉

Rank: 2Rank: 2

帖子
475
積分
967 點
潛水值
31439 米
發表於 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. }
複製代碼
...
瀏覽完整內容,請先 註冊登入會員

使用道具檢舉

Rank: 1

帖子
9
積分
31 點
潛水值
19430 米
發表於 2020-4-7 10:27 PM|顯示全部樓層
所有積分大於負-100的壞孩子,將可獲得重新機會成為懲罰生,權限跟幼兒生一樣。
本帖最後由 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.
...
瀏覽完整內容,請先 註冊登入會員
如果發覺自己無法使用一些功能或出現問題,請按重新整理一次,並待所有網頁內容完全載入後5秒才進行操作。

使用道具檢舉

Rank: 2Rank: 2

帖子
1051
積分
619 點
潛水值
27270 米
發表於 2020-4-8 09:29 AM|顯示全部樓層
老實說
樓主問的問題是甚麼? 我都搞不懂.
是要把class 的member function 傳給系統API 如 _beginthreadex(... ) 使用
還是如2,3 樓說的功能?


回覆中加入附件並不會使你增加積分,請使用主題方式發佈附件。

使用道具檢舉

您需要登錄後才可以回帖 登錄 | 註冊

Powered by Discuz!

© Comsenz Inc.

重要聲明:本討論區是以即時上載留言的方式運作,對所有留言的真實性、完整性及立場等,不負任何法律責任。而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。於有關情形下,用戶應尋求專業意見(如涉及醫療、法律或投資等問題)。 由於本討論區受到「即時上載留言」運作方式所規限,故不能完全監察所有留言,若讀者發現有留言出現問題,請聯絡我們。有權刪除任何留言及拒絕任何人士上載留言,同時亦有不刪除留言的權利。切勿上傳和撰寫 侵犯版權(未經授權)、粗言穢語、誹謗、渲染色情暴力或人身攻擊的言論,敬請自律。本網站保留一切法律權利。
回頂部