[問題] 請教 Function Pointer 的觀念

看板C_and_CPP (C/C++)作者 (2266)時間16年前 (2009/03/06 23:38), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/4 (看更多)
看紅螞蟻那本書看到Function Point 可是看不太懂 ( P 435 第五版 ) 程式的宣告那段是這樣的 void selectionSort( int work[], const int size, bool (*compare)( int, int ) ) 後面那段 bool (*compare)( int, int ) 看不懂 為何要這樣做? 也不懂什麼意思-。- 為什麼不能 bool (*compare) 就好?? 為什麼不能 bool *compare( int, int ) ?? 為什麼不能 bool compare( int, int ) ?? 裡面的一段敘述是這樣的 The text (*compare) indicates the name of the pointer to the function ( the * indicates that parameter compare is a pointer ). Parentheses are needed around *compare to indicate that compare is a pointer to a function. If we had not included the parentheses, the declaration would have been bool *compare( int, int ) which declares a function that receives two integers as parameters and returns a pointer to a bool value 可以詳細的說明嗎,還有何時需要用 Function pointer,感激不盡^^ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.62.203.138

03/06 23:53, , 1F
有一個指標,它指向一種函式:兩個int參數,回傳bool
03/06 23:53, 1F

03/07 00:09, , 2F
那本書的例子就是其中一個使用時機呀
03/07 00:09, 2F

03/07 00:11, , 3F
在執行時才決定是大->小或是小->大排序
03/07 00:11, 3F
文章代碼(AID): #19iKDa1i (C_and_CPP)
文章代碼(AID): #19iKDa1i (C_and_CPP)