[問題] template 內function pointer宣告方式

看板C_and_CPP (C/C++)作者 (累人啊....)時間13年前 (2013/01/21 15:10), 編輯推噓0(0017)
留言17則, 5人參與, 最新討論串1/1
舉個例子: class test{ void fun1(); void fun2(); template <typename T> class Traits; template <> class Traits<int>{ typedef void (*fptr)(); }; template <> class Traits<float>{ typedef void (*fptr)(); }; }; 我希望在使用時,Traits<int>::fptr 會指向fun1(); Traits<float>::fptr指向fun2(); class內我應該要怎麼改才對? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.134.238.4

01/21 15:19, , 1F
member function pointer != function pointer
01/21 15:19, 1F

01/21 15:19, , 2F
fptr的宣告方式就錯啦
01/21 15:19, 2F

01/21 15:27, , 3F

01/21 15:30, , 4F
看錯改一下...
01/21 15:30, 4F

01/21 15:33, , 5F

01/21 21:18, , 6F
謝謝,想不到還要用到繼承@@
01/21 21:18, 6F

01/21 21:22, , 7F
如果需求只是這樣不用繼承也ok啦..只是不想讓code重複
01/21 21:22, 7F

01/21 23:18, , 8F
你想要user的code長怎樣?
01/21 23:18, 8F

01/21 23:34, , 9F
用途+1 不然其實virtual function就夠了
01/21 23:34, 9F

01/21 23:43, , 10F
我想要最後呈現的方式如a大所寫的那樣
01/21 23:43, 10F

01/21 23:43, , 11F
但是今天想了一陣子不知道怎麼寫,就發文了XD
01/21 23:43, 11F

01/21 23:46, , 12F
http://ideone.com/Rtlp9s 這樣結果是一樣的
01/21 23:46, 12F

01/21 23:46, , 13F
如果你fptr沒有什麼奇妙的用途這樣簡單很多
01/21 23:46, 13F

01/21 23:53, , 14F
這樣應該比較鬆綁 https://gist.github.com/4586982
01/21 23:53, 14F

01/22 00:26, , 15F

01/22 02:16, , 16F
謝謝,學到很多,倒是版大的版本太玄了,我完全看不懂@@
01/22 02:16, 16F

01/22 02:17, , 17F
太多工具沒用過...
01/22 02:17, 17F
文章代碼(AID): #1G_Ed-0s (C_and_CPP)
文章代碼(AID): #1G_Ed-0s (C_and_CPP)