[問題] template in c

看板C_and_CPP (C/C++)作者 (一見鍾情)時間14年前 (2011/11/27 13:48), 編輯推噓0(008)
留言8則, 4人參與, 最新討論串1/1
如題 請問 當我今天有一個list(一個struct和一個function) ex: struct node{ struct node next; type data; } void add(type a); 要怎麼讓此function 知道 當tpye不同時(int,float...etc) 做不一樣的list(not overload)? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.173.31.137

11/27 13:54, , 1F
想得到的是用void *加上自己傳進型別資訊(用enum表示)
11/27 13:54, 1F

11/27 14:02, , 2F
template in c, gcc 的source code 有很噁心的範例
11/27 14:02, 2F

11/27 14:02, , 3F
大致上可以用marco兜出來
11/27 14:02, 3F

11/27 14:03, , 4F
阿, 仔細看完後覺得用union+type code應該就可以了
11/27 14:03, 4F

11/27 14:04, , 5F
對喔,union會比void *還好
11/27 14:04, 5F

11/27 14:25, , 6F
之前亂寫的:http://codepad.org/869blRps
11/27 14:25, 6F

11/28 15:11, , 7F

11/28 15:12, , 8F
感覺c要寫template,需要很深的功力
11/28 15:12, 8F
文章代碼(AID): #1EqSz6pW (C_and_CPP)
文章代碼(AID): #1EqSz6pW (C_and_CPP)