subject_20488.htm

来自「一些关于vc的问答」· HTM 代码 · 共 48 行

HTM
48
字号
<p>
序号:20488 发表者:atang 发表日期:2002-11-07 07:34:05
<br>主题:函数指针传递遇到问题,help!
<br>内容:不知在get_result中这样申明一个函数指针对不对<BR>int get_result(int a,int b,int (*compare)())<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;return(compare(a,b));<BR>}<BR><BR>int max(int a,int b)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;return((a&gt;b)?a:b);<BR>}<BR><BR>int min(int a,int b)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;return((a&lt;b)?a:b);<BR>}<BR><BR>void main(void)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;int a=2,b=3;<BR>&nbsp;&nbsp;&nbsp;&nbsp;int result;<BR>&nbsp;&nbsp;&nbsp;&nbsp;result=get_result(a,b,&max);<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;result&lt;&lt;endl;<BR>}
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:PLB 回复日期:2002-11-07 09:37:42
<br>内容:template &lt;typename T&gt; <BR>int compare(T t1,T t2)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;return t1&lt;t2?0:1;<BR>}<BR>typedef int (*COMPAREINT)(int ,int);<BR>typedef int (*COMPAREDOUBLE)(double ,double);<BR>......<BR><BR>int get_result(int a,int b,COMPAREINT p)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;return p(a,b);<BR>}<BR>上面方法可行,但是不通用,可以仿效STL中的仿函数实现函数多态<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:PLB 回复日期:2002-11-07 10:51:54
<br>内容:template &lt;typename T&gt; <BR>int compare(T t1,T t2)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;return t1&lt;t2?0:1;<BR>}<BR>template &lt;typename T&gt;<BR>class Traits<BR>{<BR>public:<BR>&nbsp;&nbsp;&nbsp;&nbsp;typedef T valueType;<BR>&nbsp;&nbsp;&nbsp;&nbsp;typedef int (*COMPARE)(valueType ,valueType);<BR>};<BR><BR>template &lt;typename T&gt; <BR>int get_result(T a,T b)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;Traits&lt;T&gt;::COMPARE p = compare;<BR>&nbsp;&nbsp;&nbsp;&nbsp;return p(a,b);<BR>}<BR><BR>以下方式通过Traits特性实现,不足之处是调用get_result()函数时必须把参数的类型显式制定,否则会出现两义性,比如:get_result(7.5,5)编译通不过,必须要get_result(7.5,(double)5)才可以
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:atang 回复日期:2002-11-08 05:18:28
<br>内容:怎么样写main函数呢,这样写好象不行:<BR>main()<BR>{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int result;<BR>&nbsp;&nbsp;&nbsp;&nbsp;COMPAREINT p;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result = get_result(1,2,p);<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf("%d",result);<BR>}
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:atang 回复日期:2002-11-10 04:50:43
<br>内容:拜托楼上的哥们,给个答复啊
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:小戟 回复日期:2002-11-11 08:53:22
<br>内容:result = get_result(1,2)就可以了,其实你没有必要那样复杂,编程的目的就是要把复杂的变成简单的,还不于多写几行。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:PLB 回复日期:2002-11-11 12:45:27
<br>内容:只要用get_result(1,2)就可以了,作摸板函数的目的就是让使用者用的时候方便些,虽然做的时候麻烦。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Tonni 回复日期:2002-11-11 14:08:14
<br>内容:PLB兄说得对,完全没有必要这样天一只脚!!<BR><BR><BR>函数指针的声明如下:<BR>[DataType] (*FunName)([ParamList])<BR><BR>比如:<BR>int get_result(int (*compare)(int a,int b));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ---------------------------&nbsp;&nbsp; <BR>横线部分即是<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?