C++函数指针用法
来源:互联网 发布:百会软件 编辑:程序博客网 时间:2024/04/30 16:16
来源:《C++ Primer Plus》(第六版)(中文版)(第7章第10节函数指针)
1、代码:
#include <iostream>//三个函数,分别返回一个数组的三个地址const double *f1(const double ar[], int n){return ar;}const double *f2(const double ar[], int n){return ar + 1;}const double *f3(const double *ar, int n){return ar + 2;}int main(){using namespace std;double av[3] = { 12.3, 45.6, 78.9 };cout << "--------------------------" << endl;cout << "using pointers to functions" << endl;const double *(*p1)(const double *, int) = f1;auto p2 = f2;//or const double *(*p2)(const double *, int) = f2;cout << "address : value" << endl;cout << (*p1)(av, 3) << ": " << *(*p1)(av, 3) << endl;cout << p2(av, 3) << ": " << *p2(av, 3) << endl;cout << "--------------------------" << endl;cout << "using an array of pointers to functions" << endl;const double *(*pa[3])(const double*, int) = { f1, f2, f3 };cout << "address : value" << endl;for (int i = 0; i != 3; i++){cout << pa[i](av, 3) << ": " << *pa[i](av, 3) << endl;}cout << "--------------------------" << endl;cout << "using a pointer to a pointer to a function" << endl;auto pb = pa;//or const double *(**pb)(const double*, int) = pa;cout << "address : value" << endl;for (int i = 0; i != 3; i++){cout << pb[i](av, 3) << ": " << *pb[i](av, 3) << endl;}cout << "--------------------------" << endl;cout << "using pointers to an array of pointers" << endl;auto pc = &pa;//or const double *(*(*pc)[3])(const double *, int) = &pa;const double *pcs = (*pc)[1](av, 3);cout << "address : value" << endl;cout << (*pc)[0](av, 3) << ": " << *(*pc)[0](av, 3) << endl;cout << pcs << ": " << *pcs << endl;cout << (*(*pc)[2])(av, 3) << ": " << *(*(*pc)[2])(av, 3) << endl;cout << "--------------------------" << endl;return 0;}2、测试结果:
--------------------------using pointers to functionsaddress : value001DF97C: 12.3001DF984: 45.6--------------------------using an array of pointers to functionsaddress : value001DF97C: 12.3001DF984: 45.6001DF98C: 78.9--------------------------using a pointer to a pointer to a functionaddress : value001DF97C: 12.3001DF984: 45.6001DF98C: 78.9--------------------------using pointers to an array of pointersaddress : value001DF97C: 12.3001DF984: 45.6001DF98C: 78.9--------------------------
0 0
- c++/c 函数指针用法
- C函数指针的用法
- C函数指针的用法
- c 语言函数指针用法
- 函数指针的用法(C++)
- 【C/C++】C函数指针用法
- C/C++函数指针,指针函数的用法,用处
- C/C++函数指针和函数指针数组用法
- c语言函数指针的用法
- (转) C里函数指针用法小结
- C语言中函数指针的用法
- typedef函数指针的用法(C++)
- C&&C++中函数指针用法示例
- typedef函数指针的用法(C++)
- c/c++ 函数指针的用法
- typedef函数指针的用法(C++)
- c/c++ 函数指针的用法
- C/C++函数指针用法总结
- 《Linux内核Makefile分析》之 auto.conf, auto.conf.cmd, autoconf.h(转载)
- SharePoint 2013 Open File Explorer Error
- 第1次上机实验-2
- 《明朝那些事儿》心得体会
- PAT-Pop Sequence (25)
- C++函数指针用法
- Redis 实践
- 打印中,设备坐标与逻辑坐标转换
- http 请求 详解 1
- PCA (主成分分析)详解 (写给初学者) 结合matlab
- C++第二次实验
- PHP-Zend引擎剖析之CV变量
- ES6中Set和WeakSet的使用
- 数字分割