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;
}
相关文章推荐
- 9.26做题-队列安排
- C++输入cout与输出cin
- C++程序设计陷阱读书笔记(一)
- c++继承中的构造与析构
- 9.23/9.26强连通分量学习笔记
- 文章标题
- C语言解两个乒乓球队赛事问题
- 无法使用sort给vector排序
- More Effective C++ 条款25 将constructor和non-member function虚化
- c++ 日志类 线程安全+缓存
- C语言程序初体验-第十一课-第二题:分离浮点数的整数和小数
- C++中利用libxl操作Excel表格
- Python与C/C++ 代码互调
- [C++]memcpy 小记
- [C++]memcpy 小记
- c++设计模式-----中介者模式Mediator
- 关于素数判定的算法优化
- 《Effective C++》读书笔记(四)
- 浅谈C/C++的浮点数在内存中的存储方式
- C/C++中获取数组的长度