您的位置:首页 > 编程语言 > C语言/C++

C语言函数指针的例子

2015-09-26 22:41 381 查看
[code]#include <iostream>
using namespace std;
[/code]
//定义一个类型别名 Fuction:参数为int,返回值为void
typedef void(*Fuction)(int);
void positive(int n)
{
cout <<n <<"is a positive number" << endl;
}
void negative(int n)
{
cout << n<<" is a negative number" << endl;
}
void zero(int n)
{
cout << n << " is zero" << endl;
}
void testCallBack(int n,Fuction *PF)
{
if (n==0)
  {
*PF = zero;
}else if (n > 0)
  {
*PF = positive;
  }
else
  {
*PF = negative;
  }
(*PF)(n);
}
int main()
{
  Fuction  mFuction;      //声明一个函数指针。   
int a = 3;
int b = 0;
 
testCallBack(a, &mFuction);
testCallBack(b, &mFuction);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: