函数指针
2015-12-05 16:43
127 查看
1.函数指针
void square(int& refX, int& refY)
{
cout << "square..." << endl;
}
void cube(int& refX, int& refY)
{
cout << "cube..." << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
void(*pFunc)(int&, int&);
int oneVal = 12, twoVal = 45;
pFunc = square;
pFunc(oneVal, twoVal);
pFunc = cube;
pFunc(oneVal, twoVal);
}
2.函数指针数组
void(*pFunc[2])(int&, int&);
int oneVal = 12, twoVal = 45;
pFunc[0] = square;
pFunc[1] = cube;
pFunc[0](oneVal, twoVal);
pFunc[1](oneVal, twoVal);3.将函数指针传递给其他函数
void square(int& refX, int& refY)
{
cout << "square..." << endl;
}
void cube(int& refX, int& refY)
{
cout << "cube..." << endl;
}
void printValues(void(*)(int&, int&), int x, int y);//声明
void printValues(void(*pFunc)(int&, int&), int x, int y)//实现
{
pFunc(x, y);
}
int _tmain(int argc, _TCHAR* argv[])
{
printValues(square, 12, 23);
printValues(cube, 1, 2);
}
4.用typedef简化函数指针的定义
typedef void(*pFunc)(int&, int&); //typedef定义
void square(int& refX, int& refY)
{
cout << "square..." << endl;
}
void cube(int& refX, int& refY)
{
cout << "cube..." << endl;
}
void printValues(pFunc func, int x, int y);//声明
void printValues(pFunc func, int x, int y)//实现
{
func(x, y);
}
int _tmain(int argc, _TCHAR* argv[])
{
printValues(square, 12, 23);
printValues(cube, 1, 2);
}5.指向类成员函数的函数指针
class Dog
{
public:
Dog(){};
~Dog(){};
void speak(int&, int&) const{ cout << "Dog speak!" << endl; }
private:
};
int _tmain(int argc, _TCHAR* argv[])
{
void(Dog::*pClassFunc)(int&, int&) const = 0; //定义
pClassFunc = &Dog::speak; //赋值
int oneVal = 12, twoVal = 9;
Dog dog;
(dog.*pClassFunc)(oneVal, twoVal);//调用
}
void square(int& refX, int& refY)
{
cout << "square..." << endl;
}
void cube(int& refX, int& refY)
{
cout << "cube..." << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
void(*pFunc)(int&, int&);
int oneVal = 12, twoVal = 45;
pFunc = square;
pFunc(oneVal, twoVal);
pFunc = cube;
pFunc(oneVal, twoVal);
}
2.函数指针数组
void(*pFunc[2])(int&, int&);
int oneVal = 12, twoVal = 45;
pFunc[0] = square;
pFunc[1] = cube;
pFunc[0](oneVal, twoVal);
pFunc[1](oneVal, twoVal);3.将函数指针传递给其他函数
void square(int& refX, int& refY)
{
cout << "square..." << endl;
}
void cube(int& refX, int& refY)
{
cout << "cube..." << endl;
}
void printValues(void(*)(int&, int&), int x, int y);//声明
void printValues(void(*pFunc)(int&, int&), int x, int y)//实现
{
pFunc(x, y);
}
int _tmain(int argc, _TCHAR* argv[])
{
printValues(square, 12, 23);
printValues(cube, 1, 2);
}
4.用typedef简化函数指针的定义
typedef void(*pFunc)(int&, int&); //typedef定义
void square(int& refX, int& refY)
{
cout << "square..." << endl;
}
void cube(int& refX, int& refY)
{
cout << "cube..." << endl;
}
void printValues(pFunc func, int x, int y);//声明
void printValues(pFunc func, int x, int y)//实现
{
func(x, y);
}
int _tmain(int argc, _TCHAR* argv[])
{
printValues(square, 12, 23);
printValues(cube, 1, 2);
}5.指向类成员函数的函数指针
class Dog
{
public:
Dog(){};
~Dog(){};
void speak(int&, int&) const{ cout << "Dog speak!" << endl; }
private:
};
int _tmain(int argc, _TCHAR* argv[])
{
void(Dog::*pClassFunc)(int&, int&) const = 0; //定义
pClassFunc = &Dog::speak; //赋值
int oneVal = 12, twoVal = 9;
Dog dog;
(dog.*pClassFunc)(oneVal, twoVal);//调用
}
相关文章推荐
- 函数指针
- 【已解决】Android Studio项目编译出错,可以运行
- [探测与发现]星际之门计划
- 另一篇并查集
- sql加索引和删除索引,提高效率等
- 宏与内联函数的区别
- Const #define
- DB Initialization(数据库初始化)[EF Code-First系列]
- LeetCode Unique Binary Search Trees
- DHCP及DHCP中继的配置
- 设计模式(十二)责任链模式
- Android6.0 源码编译 + AS阅读源码
- [电影与电视]CCTV为什么没有科幻频道呢?
- 并查集(Union-Find)算法介绍
- 安卓开发遇到的问题
- 打印调试信息
- [LeetCode] Super Ugly Number (Medium)
- ubuntu下nginx的启停等常用命令
- 编写实现连接oracle数据库并返回Connection对象的Java工具类
- 生活小百科