作者jimmy2266 (2266)
看板C_and_CPP
標題[問題] 請教 Function Pointer 的觀念
時間Fri Mar 6 23:38:07 2009
看紅螞蟻那本書看到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
1F:→ james732:有一個指標,它指向一種函式:兩個int參數,回傳bool 03/06 23:53
2F:推 stonehomelaa:那本書的例子就是其中一個使用時機呀 03/07 00:09
3F:→ stonehomelaa:在執行時才決定是大->小或是小->大排序 03/07 00:11