POINTERS TO FUNCTIONS
#include "stdio.h"
int add(int a, int b)
{
return (a+b);
}
int sub(int a, int b)
{
return (a-b);
}
int calcu(int a, int b, int (*f)(int , int))
{
int result;
result=(*f)(a,b);
return result;
}
int main()
{
int m,n;
m=calcu(7,5,add);
n=calcu(10,2,sub);
int (*minus)(int,int)=sub;
int p;
p=calcu(20,6,minus);
printf("m=%d\nn=%d\np=%d\n",m,n,p);
return 0;
}
/**
OCS101:~/cpl # gcc pointerToFuntions.c
OCS101:~/cpl # ./a.out
m=12
n=8
p=14
*/
C++ allows operations with pointers to functions. The typical use of this is for passing a function as an argument to another function, since these cannot be passed dereferenced. In order to declare a pointer to a function we have to declare it like the
prototype of the function except that the name of the function is enclosed between parentheses
() and an asterisk (*) is inserted before the name:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
// pointer to functions
#include <iostream>
using namespace std;
int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
int (*minus)(int,int) = subtraction;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
|
8
|
In the example, minus is a pointer to a function that has two parameters of type
int. It is immediately assigned to point to the function subtraction, all in a single line:
|
int (* minus)(int,int) = subtraction;
|
分享到:
相关推荐
ARKit by Tutorials: Building Augmented Reality Apps in Swift 4.2, 2nd Edition by Chris Language-2019(pdf)-分卷4-共5分卷 解压密码:19990323 ARKit by Tutorials: Building Augmented Reality Apps in ...
ARKit by Tutorials: Building Augmented Reality Apps in Swift 4.2, 2nd Edition by Chris Language-2019(pdf)-分卷3-共5分卷 解压密码:19990323 ARKit by Tutorials: Building Augmented Reality Apps in ...
Machine Learning by Tutorials: Beginning machine learning for Apple and iOS by Matthijs Hollemans-- May 31, 2019 Machine Learning by Tutorials: Beginning machine learning for Apple and iOS by ...
Machine Learning by Tutorials: Beginning machine learning for Apple and iOS by Matthijs Hollemans-- May 31, 2019 Machine Learning by Tutorials: Beginning machine learning for Apple and iOS by ...
Machine Learning by Tutorials: Beginning machine learning for Apple and iOS by Matthijs Hollemans-- May 31, 2019 Machine Learning by Tutorials: Beginning machine learning for Apple and iOS by ...
ARKit by Tutorials: Building Augmented Reality Apps in Swift 4.2, 2nd Edition by Chris Language-2019(pdf)-分卷2-共5分卷 解压密码:19990323 ARKit by Tutorials: Building Augmented Reality Apps in ...
ARKit by Tutorials: Building Augmented Reality Apps in Swift 4.2, 2nd Edition by Chris Language-2019(pdf)-分卷1-共5分卷 解压密码:19990323 ARKit by Tutorials: Building Augmented Reality Apps in ...
ARKit by Tutorials: Building Augmented Reality Apps in Swift 4.2, 2nd Edition by Chris Language-2019(pdf)-分卷0-共5分卷 解压密码:19990323 ARKit by Tutorials: Building Augmented Reality Apps in ...
ARKit by Tutorials: Building Augmented Reality Apps in Swift 4.2, 2nd Edition by Chris Language English | 2019 | ISBN: 1942878711 | 496 Pages | True PDF, CODE | 1623 MB Learn ARKit, Apple’s New ...
ARKit by Tutorials: Building Augmented Reality Apps in Swift 4.2, 2nd Edition by Chris Language English | 2019 | ISBN: 1942878711 | 496 Pages | True PDF, CODE | 1623 MB Learn ARKit, Apple’s New ...
Metal by Tutorials: Beginning game engine development with Metal by Caroline Begbie English | 2019 | ISBN: 1942878544 | 659 Pages | True PDF, EPUB, CODE | 790 MB Build your own low-level game engine ...
Metal by Tutorials: Beginning game engine development with Metal by Caroline Begbie English | 2019 | ISBN: 1942878544 | 659 Pages | True PDF, EPUB, CODE | 790 MB Build your own low-level game engine ...
Metal by Tutorials: Beginning game engine development with Metal by Caroline Begbie English | 2019 | ISBN: 1942878544 | 659 Pages | True PDF, EPUB, CODE | 790 MB Build your own low-level game engine ...
ARKit by Tutorials: Building Augmented Reality Apps in Swift 4.2, 2nd Edition by Chris Language English | 2019 | ISBN: 1942878711 | 496 Pages | True PDF, CODE | 1623 MB Learn ARKit, Apple’s New ...
ARKit by Tutorials: Building Augmented Reality Apps in Swift 4.2, 2nd Edition by Chris Language English | 2019 | ISBN: 1942878711 | 496 Pages | True PDF, CODE | 1623 MB Learn ARKit, Apple’s New ...
ARKit by Tutorials: Building Augmented Reality Apps in Swift 4.2, 2nd Edition by Chris Language English | 2019 | ISBN: 1942878711 | 496 Pages | True PDF, CODE | 1623 MB Learn ARKit, Apple’s New ...
openmm-tutorials:快速入门Python教程,帮助分子动力学从业人员快速掌握OpenMM
With its innovative approach to teaching the language, Accelerated C++ will challenge readers in the right way. It suggests that you don’t need to learn C to be productive in C++. Written in an ...