函数指针是一种非常好的类型。因此,可以编写一个函数,它的一个参数是函数指针。然后,在(外部)函数使用其函数指针参数时,就间接地调用在调用函数时对应参数指向的函数。
由于指针在不同的情况下可以指向不同的函数,因此允许调用程序确定要从外部函数中调用哪个函数。
在用函数指针类型的参数调用函数时,参数可以只包含函数地址的相应类型的指针。还可以把函数名作为参数,显示传送函数。作为参数传送给另一个函数的函数有时称为回调函数。
示例:
#include
using std::cout;
using std::endl;
//函数声明
double squared(double);
double cubed(double);
double sum_array(double array[],int len,double (*pfun) (double));
int main(){
double array[]={1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5};
int len=sizeof array/sizeof array[0];
cout<<"Sum of squares = "<
-
<script type="text/java script">BAIDU_CLB_fillSlot("771048");
-
点击复制链接 与好友分享!
回本站首页
<script>
function copyToClipBoard(){
var clipBoardContent=document.title + '\r\n' + document.location;
clipBoardContent+='\r\n';
window.clipboardData.setData("Text",clipBoardContent);
alert("恭喜您!复制成功");
}
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];