C++函数传参、函数指针的定义以及调用
来源:互联网 发布:isp图像处理编程 编辑:程序博客网 时间:2024/05/01 19:01
方法
声明
bool (*bigger)(const int& a, const int& b);
需要说明函数的传参以及返回值类型。
赋值
bigger = BiggerThan;
或者
bigger = &BiggerThan;
可以通过函数的名称或地址赋值。
调用
bigger(3,4)(*bigger)(5, 2)
上面2种方法都行
代码
#include<iostream>#include<exception>using namespace std;bool BiggerThan(const int& a, const int& b){ return a > b;}int main(){ cout << "start!" << endl; bool (*bigger)(const int& a, const int& b); bigger = BiggerThan; cout << bigger(2,1) << endl; bigger = &BiggerThan; cout << bigger(3,4) << endl; cout << (*bigger)(5, 2) << endl; cout << "end!" << endl; system("pause"); return EXIT_SUCCESS;}
函数传参
- 值类型传递的时候,在函数中修改该值之后,值仍然不会改变,但是引用传递的话,会发生改变
int
、float
、等都是值类型,但是数组、类等就是引用类型,如果不想让函数修改引用类型的变量,可以在前面加上const
限定符,如果要将值传递类型的变量改变为引用传递,在函数传参的定义前面加上&就行。
代码
#include<iostream>#include<exception>using namespace std;bool BiggerThan(const int& a, const int& b){ return a > b;}void RefTest(int &a){ a++;}void RefTestArray(int a[]){ a[0] = 22;}int main(){ cout << "start!" << endl; int b = 1; cout << b << endl; RefTest(b); cout << b << endl; cout << "array : " << endl; int c[3] = {3,4,5}; cout << c[0] << endl; RefTestArray( c ); cout << c[0] << endl; cout << "end!" << endl; system("pause"); return EXIT_SUCCESS;}
0 0
- C++函数传参、函数指针的定义以及调用
- C语言:返回指针的函数,函数声明、定义和调用,嵌套调用
- 函数指针以及函数指针数组的定义与使用
- 【c和指针学习笔记】函数指针的初始化以及函数调用的三种方式
- 函数指针与指针函数,以及它们的调用
- c语言函数指针的定义
- c语言函数指针的定义
- C语言函数调用时参数压栈的顺序以及函数指针的作用
- 函数指针的定义
- [C#]调用字符串中定义的函数
- C语言函数指针定义
- 漫谈C指针第十七讲---定义函数的指针类型
- 关于C的指针函数,函数指针,函数指针数组调用的一些理解
- 函数指针的特殊性, 定义和调用都有两种做法
- c 函数指针 和 返回函数指针的函数 的定义方法
- 函数指针与指针函数以及结构体调用函数的方法
- 函数指针与指针函数以及结构体调用函数的方法
- c语言中的函数的定义以及相关的调用、嵌套、递归以及和数组的关系
- (三)React Native---TextInput
- 编程心得04
- ti81xx_interrupt 1146: VBUS error workaround (delay coming)产生原因分析
- 371. Sum of Two Integers
- 修复U盘(未完)
- C++函数传参、函数指针的定义以及调用
- 离散傅里叶变换的推导
- HDG学习笔记
- Android开发中关于Spinner;Checkbox; RadioButton; EditText;怎样向其他Ativity传值
- 2016/10/31
- 图像分割结果的评估
- three.js环境下的一种飞线动画实现方法
- 网络编程(30)—— IP地址分类和子网掩码的作用
- JAVA中==和equals的区别