[問題] 傳入一個function pointer

看板C_and_CPP (C/C++)作者 (DaZe~☆)時間14年前 (2012/04/17 23:45), 編輯推噓0(009)
留言9則, 3人參與, 最新討論串1/1
小弟目前正在使用C++ 的一套Graphcut Lib MRF 裡面在呼叫mrf這個class的時候是長這樣: DataCost *data = new DataCost(DataTerm); //DataTerm 是一個int指標 SmoothnessCost *smooth = new SmoothnessCost(Smoothness); //這裡是我的問題 //Smoothness 是一個回傳 float的function 要求是長這樣 float foo(int a, int b, int c, int d) //以下不重要.... EnergyFunction *eng = new EnergyFunction(data,smooth); //For Graph-cuts with expansion algorithm: float t; MRF* mrf = new Expansion(Width,Height,2,eng); 我想要把這段code包到一個class A中去使用 但是我在A之中寫了一些public function float A_Smooth(int a, int b, int c, int d) {return xxx;}; void Do() { DataCost *data = new DataCost(DataTerm); SmoothnessCost *smooth = new SmoothnessCost(A_Smooth); EnergyFunction *eng = new EnergyFunction(data,smooth); //For Graph-cuts with expansion algorithm: float t; MRF* mrf = new Expansion(Width,Height,2,eng); mrf->initialize(); ... }; 會出現compile error: function call missing argument list; use '&A::A_Smooth' to create a pointer to member 目前的問題是 A 之中有一個物件 a_obj負責提供資料給A_Smooth 如果把這個物件設為Static 然後把A_Smooth拿到 class外面變成普通funciton 變成 float A_Smooth(int a, int b, int c, int d) {return A::obj[a][b];} 就可以正確compile, 但是這樣同時間只能有一個class A 呼叫 Do(), 請問有辦法保持讓A_Smooth放在class A之中 但還是能成功Compile的寫法嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.34.171.29

04/18 00:08, , 1F
如果可以的話改用 std::function + std::bind
04/18 00:08, 1F

04/18 00:09, , 2F
不行的話要想辦法做 wrapper
04/18 00:09, 2F

04/18 01:01, , 3F
bind似乎是vs2010的 我沒辦法用 可以請教wrapper有甚麼
04/18 01:01, 3F

04/18 01:01, , 4F
推薦的資料可以參考嗎 謝謝
04/18 01:01, 4F

04/18 11:58, , 5F
SmoothnessCost用cdecl當參數,但是A的A_Smooth為thicall所
04/18 11:58, 5F

04/18 11:59, , 6F
以你在A要用&A::A_Smooth,然後把A_Smooth改成static就可以
04/18 11:59, 6F

04/18 12:00, , 7F
要不然就拿出來寫。
04/18 12:00, 7F

04/18 15:03, , 8F
可以用 boost 的版本, 或是在 stack overflow 找關鍵字
04/18 15:03, 8F

04/18 15:03, , 9F
C callback member function, 有不少 workaround
04/18 15:03, 9F
文章代碼(AID): #1FZP0Ykq (C_and_CPP)
文章代碼(AID): #1FZP0Ykq (C_and_CPP)