函数指针的使用例子
来源:互联网 发布:网络诈骗罪的量刑标准 编辑:程序博客网 时间:2024/06/01 10:41
#include <windows.h>
int outputA(int );
int outputB(int );
void outputC(int );
void outputD();
void outputE();
int _tmain(int argc, _TCHAR* argv[])
{
int(*pfunA)(int);
int(*pfunB)(int);
pfunA=outputA;
pfunA(1);
pfunB=outputB;
(*pfunB)(1);
typedef void(*pfunC)(int);
pfunC defFunc;
defFunc=outputC;
defFunc(1);
int(*PfunD[2])(int);
//PfunD={outputA,outputB};
PfunD[0]=outputA;
PfunD[1]=outputB;
PfunD[0](0);
PfunD[1](1);
typedef void (*pfunE[2])();
pfunE p[]={outputD,outputE};
for (int i=0;i<=1;i++)
{
p[0]();
}
return 0;
}
int outputA(int a)
{
OutputDebugString(L" outputA \n");
return 0;
}
int outputB(int b)
{
OutputDebugString(L" outputB \n");
return 0;
}
void outputC(int a)
{
OutputDebugString(L" outputC \n");
}
void outputD()
{
OutputDebugString(L" outputD\n");
}
void outputE()
{
OutputDebugString(L" outputE\n");
}
输出:
outputA
outputB
outputC
outputA
outputB
2、
#include "stdafx.h"
#include "windows.h"
#include<iostream>
using namespace std;
void site1()
{
OutputDebugStringA("www.ok2002.com \n");
}
void site2()
{
OutputDebugStringA("www.ok1700.com \n");
}
void site3()
{
OutputDebugStringA("www.ok1500.com \n");
}
void main()
{
typedef void (*p)();
p v[]={site1,site2,site3};
int size_v=sizeof(v)/sizeof(v[0]);
cout<<"size_v="<<size_v<<endl;
int i=0;
for(;i<size_v;++i)
v[i]();
}
输出:
www.ok2002.com
www.ok1700.com
www.ok1500.com
3、
#include "stdafx.h"
#include <windows.h>
int outputA(int );
int outputB(int );
void outputC(int );
void outputD();
void outputE();
int _tmain(int argc, _TCHAR* argv[])
{
int(*pfunA)(int);
int(*pfunB)(int);
pfunA=outputA;//直接使用
pfunA(1);
pfunB=outputB;
(*pfunB)(1);//前面加*使用
typedef void(*pfunC)(int);
pfunC defFunc;
defFunc=outputC;//通过类型重命名使用
defFunc(1);
int(*PfunD[2])(int);//定义函数数组使用
//PfunD={outputA,outputB};
PfunD[0]=outputA;
PfunD[1]=outputB;
PfunD[0](0);
PfunD[1](1);
// typedef void (*pfunE[2])( );//声明错误,不能开始定义大小
// pfunE pD[]={outputD,outputE};
// for (int i=0;i<=1;i++)
//{
// pD[0]( );
// }
void (*pfunE[])( )={outputD,outputE};;//定义时便赋值变量,初始化
for (int i=0;i<=1;i++)
{
pfunE[i]( );
}
typedef void (*p)();//定义函数数组使用
p v[]={outputD,outputE};
int size_v=sizeof(v)/sizeof(v[0]);
//cout<<"size_v="<<size_v<<endl;
int i=0;
for(;i<size_v;++i)
v[i]();
return 0;
}
int outputA(int a)
{
OutputDebugString(L" outputA \n");
return 0;
}
int outputB(int b)
{
OutputDebugString(L" outputB \n");
return 0;
}
void outputC(int a)
{
OutputDebugString(L" outputC \n");
}
void outputD()
{
OutputDebugString(L" outputD\n");
}
void outputE()
{
OutputDebugString(L" outputE\n");
}
输出:
outputA
outputB
outputC
outputA
outputB
outputD
outputE
outputD
outputE
- 使用函数指针的例子
- 函数指针的使用例子
- 函数指针使用例子
- 指针函数和函数指针的使用例子
- C++ 使用成员函数指针的一个例子
- C / C++ 类中使用函数指针的小例子
- typedef 函数指针的使用(含例子)
- 指针返回 rand()函数的使用例子
- 函数指针的两个例子,
- 指针变量函数的例子
- 函数指针简单的例子
- 函数指针数组的例子
- 函数指针的两个例子
- 函数指针的一个例子
- 函数指针数组的例子
- 函数指针数组的例子
- 函数指针的几个例子
- 指针的指针的实际使用例子
- 重写toString方法需要注意的
- 2 在界面上显示战机
- 9.6
- css sprite 介绍和实例
- 2011.09.05
- 函数指针的使用例子
- 技术博客—IO流部分
- poj 3517(约瑟夫环。。。。。)
- 2011.9技术博客IO包中的其他对象
- vi学习_3学习vi命令的策略
- EEPROM、Flash学习总结 (MC9S12DG128)
- 指针的魅力
- Android开发的前景
- 串口学习总结(基于MC9SDG128)