Re: [問題] 請教 Function Pointer 的觀念
看板C_and_CPP (C/C++)作者sawang (New Friends)時間16年前 (2009/03/07 01:21)推噓0(0推 0噓 1→)留言1則, 1人參與討論串2/4 (看更多)
※ 引述《jimmy2266 (2266)》之銘言:
: 為什麼不能 bool (*compare) 就好??
這樣沒有宣告你要的函數需要幾個parameters
: 為什麼不能 bool *compare( int, int ) ??
這樣那個*號變成是跟bool連在一起的,表示回傳值是指向bool型態的指標
而不是表示compare是function pointer
: 為什麼不能 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,感激不盡^^
這段課文應該是強調function pointer的語法怎麼寫
正確的寫法是這樣 bool (*compare)(int,int)
表示一個函數他有兩個int參數,以及一個bool的回傳值
簡單想,你可以把 (*compare) 整個換成一個函式名稱,例如 compMax
另外他又說,不可以把括號去掉,否則會變成
bool* compare(int,int)
這樣compare就不是一個function pointer,而是一個函式名
然後這個涵式他有兩個int參數,以及一個指向bool型態pointer的回傳值
其實課本例子就是個很好的使用function pointer的時機
但我自己很少用過這種語法來寫程式,除了trace一些system program的code以外
希望有替你解惑到
--
當我喜歡你的時候
我不會讓自己後悔
更不會讓你後悔
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.44.176.1
→
03/07 10:53, , 1F
03/07 10:53, 1F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章