作者sawang (New Friends)
看板C_and_CPP
标题Re: [问题] 请教 Function Pointer 的观念
时间Sat Mar 7 01:21:50 2009
※ 引述《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
1F:→ realmeat:bool compar(int, int); 这样写是宣告prototype 03/07 10:53