ca71a_c++_指向函数的指针_通过指针调用函数txwtech
/*ca71a_c++_指向函数的指针_通过指针调用函数
用typedef简化函数指针的定义
简化前:
bool(*pf)(const string&, const string &);
bool(*pf2)(const string&, const string &);
bool(*pf3)(const string&, const string &);
简化后:
typedef bool(*cmpFcn)(const string &, const string &);
//具体应用参考main函数内部代码
comFcn pf;
comFcn pf2;
comFcn pf3;
指向函数的指针的初始化和赋值
通过指针调用函数
函数指针形参
返回指向函数的指针
//ff是一个函数,有一个形参x,返回的结果是一个函数指针int(*)(int *,int),
int(*ff(int x))(int *, int)
{
cout << x << endl;
return demo;
}
调用时,首先执行ff(int x),然后返回*int的指针,然后指向demo,然后把demo(int *, int)传递给demo函数的定义
int demo(int *p, int a)
{
return 8;
}
使用typedef简化,一遍更好的理解:
typedef int (*PF)(int *, int);
PF ff(int x)
{
cout << x << endl;
return demo;//指针指向demo,调用demo函数
}
//pf是一个指针,指向函数的指针:函数类型
bool(*pf)(const string&, const string &);
//
//bool *pf(const string&, const string &);
*pf如果没有括号,pf就是一个函数了,它的返回值是指向bool的一个指针
返回类型不一样,或者形参不一样,也不可以指向
指向重载函数的指针:
//void(*pff)(int) = &ff;//需要精确匹配
void (*pfg)(vector<double>) = &ff;
//double(*pfg1)(vector<double>) = &ff;//错误,类型不匹配
void(*pfg2)(unsigned int) = &ff;
*/
/*ca71a_c++_指向函数的指针_通过指针调用函数
用typedef简化函数指针的定义
简化前:
bool(*pf)(const string&, const string &);
bool(*pf2)(const string&, const string &);
bool(*pf3)(const string&, const string &); 简化后:
typedef bool(*cmpFcn)(const string &, const string &);
//具体应用参考main函数内部代码
comFcn pf;
comFcn pf2;
comFcn pf3;
指向函数的指针的初始化和赋值
通过指针调用函数
函数指针形参
返回指向函数的指针 //ff是一个函数,有一个形参x,返回的结果是一个函数指针int(*)(int *,int),
int(*ff(int x))(int *, int) {
cout << x << endl;
return demo;
}
调用时,首先执行ff(int x),然后返回*int的指针,然后指向demo,然后把demo(int *, int)传递给demo函数的定义
int demo(int *p, int a)
{
return 8;
}
使用typedef简化,一遍更好的理解:
typedef int (*PF)(int *, int); PF ff(int x)
{
cout << x << endl;
return demo;//指针指向demo,调用demo函数
} //pf是一个指针,指向函数的指针:函数类型
bool(*pf)(const string&, const string &);
//
//bool *pf(const string&, const string &);
*pf如果没有括号,pf就是一个函数了,它的返回值是指向bool的一个指针 返回类型不一样,或者形参不一样,也不可以指向 指向重载函数的指针: //void(*pff)(int) = &ff;//需要精确匹配
void (*pfg)(vector<double>) = &ff;
//double(*pfg1)(vector<double>) = &ff;//错误,类型不匹配
void(*pfg2)(unsigned int) = &ff; */ #include <iostream>
#include <string>
#include <vector>
using namespace std; //函数定义简化操作
typedef bool(*cmpFcn)(const string &, const string &);
typedef int (*PF)(int *,int); bool lengthCompare(const string &s1, const string &s2)
{
return s1.size() == s2.size();//比较长度
}
string::size_type sumlength(const string &s1, const string &s2)
{
return s1.size() + s2.size();//长度相加
}
bool cstringCompare(char *s1, char *s2)
{
return strlen(s1) == strlen(s2);
}
void useBigger(const string &s1, const string &s2, bool(*pf)(const string &, const string &))//函数指针形参
{
cout << pf(s1, s2) << endl;
}
int demo(int *p, int a)
{
cout << "执行demo()" << endl;
return ;
}
//ff是一个函数,有一个形参x,返回的结果是一个函数指针int(*)(int *,int),
//int(*ff(int x))(int *, int)//这句也可以
PF ff(int x)//经过typedef简化后:
{
cout << "执行ff(int x)" << endl;
cout << x << endl;
return demo;
}
//指向重载函数的指针: void ff(vector<double> vec)
{
cout << "ff(vector<double> vec)" << endl;
}
void ff(unsigned int x)
{
cout << "ff(unsigned int x)" << endl;
}
int main()
{
int a = ;
int *pa;
//pf是一个指针,指向函数的指针:函数类型
bool(*pf)(const string&, const string &);//pf是一个局部变量
bool(*pf2)(const string&, const string &);
bool(*pf3)(const string&, const string &);
pf = &lengthCompare;//也可以写成:pf=lengthCompare; pa = &a;//普通指针
cout << lengthCompare("hello", "pointer") << endl;
cout << lengthCompare("hello", "point") << endl;
cout << (*pf)("hello", "point") << endl;//函数指针
//也可以写成这样:*号省掉
cout << pf("hello", "point") << endl;//函数指针 cout << *pa << endl; //bool(*pf)(const string&, const string &);被简化为如下:
cmpFcn pfa;
cmpFcn pf2a = ;//指向函数的指针的初始化
cmpFcn pf3a = ;
pf3 = pf2a;//指向函数的指针的赋值
//然后调用方法一样:
pfa = &lengthCompare; //指向函数的指针的赋值
pf2 = lengthCompare;
//pf3 = sumlength; //指针指向的返回值需要一致。不能bool指向 size_type.
cout << (*pfa)("hello", "point") << endl;//函数指针
cout << pf2("bb","cc") << endl;//也可以写成这样:*号省掉 cmpFcn pf4 = lengthCompare;
useBigger("hi","function",pf4);
cout << ff()(&a,a) << endl; //指向重载函数的指针: //void(*pff)(int) = &ff;//需要精确匹配
void (*pfg)(vector<double>) = &ff;
//double(*pfg1)(vector<double>) = &ff;//错误,类型不匹配
void(*pfg2)(unsigned int) = &ff;
return ;
}
ca71a_c++_指向函数的指针_通过指针调用函数txwtech的更多相关文章
- 指针的指针&指向指针数组的指针
一.指针的指针 指针的指针看上去有些令人费解.它们的声明有两个星号.例如: char ** cp; 如果有三个星号,那就是指针的指针的指针,四个星号就是指针的指针的指针的指针 ...
- Python笔记:调用函数,带扩号和和不带括号的区别
调用函数,如果带括号,那么是调用函数运行后的结果, 调用函数不带括号,调用的是函数本身 例如: def cun (a,b): return a+b print(cun) : 调用函数,打印的是函数 p ...
- python 定义函数 调用函数
创建test.py文件 #coding=utf-8 #定义函数 def hello(): print "hello world" #调用函数 hello() 在cmd下面运行
- Python:函数的命名空间、作用域与闭合函数
1,参数陷阱 如果默认参数的只是一个可变数据类型,那么每一次调用的时候,如果不传值就共用这个数据类型的资源. 2,三元运算 c=a if a>b else b#如果a>b返回a,否则,返回 ...
- js笔记(4)--关于在window.onload()里面定义函数,调用函数无法执行~
由于本人学习js学不久,所以,今天刚好遇到了一个关于在window.onload里面定义函数,然后在html里面调用函数时出现错误.具体见下面: <!DOCTYPE html> <h ...
- python之函数名称空间,作用域,嵌套函数
目录 嵌套函数 定义 名称空间的三大类(只存变量名) 名称空间加载顺序 变量名的查找顺序为 作用域 嵌套函数 定义 函数内部定义的函数,无法在函数外部使用内部定义的函数. def f1(): def ...
- C++第四篇--重载_指针_引用
C++第四篇--重载_指针_引用 1. 基础知识 重载:函数名相同,根据参数不同(类型.数量.顺序不同)调用同名函数 指针和引用:引用就是别名,引用时必须初始化,引用你定义的变量. int a; in ...
- c++指向指针的指针与 c++指针作为函数参数传递问题
一直搞不明白,c++中指针到底是个啥东西,今天遇到到c++,指向指针的指针的问题,突然有点开窍了. 举个例子: int main(int argc, char** argv){ int a[5]={1 ...
- 零基础逆向工程24_C++_01_类_this指针_继承本质_多层继承
1 类内的成员函数和普通函数的对比 1.1 主要是从参数传递.压栈顺序.堆栈平衡来总结. 1.参数传递:成员函数多传一个this指针 2.压栈顺序:成员函数会将this指针压栈,在函数调用取出 3.堆 ...
随机推荐
- SimpleAuthenticationInfo的参数
SimpleAuthenticationInfo的参数 仅供个人参考,以及学习记录.SimpleAuthenticationInfo authenticationInfo = new SimpleAu ...
- 深入理解JS:执行上下文中的this(一)
目录 执行上下文与执行上下文栈 this 全局环境 函数环境 总结 参考 1.执行上下文与执行上下文栈 (1)什么是执行上下文? 在 JavaScript 代码运行时,解释执行全局代码.调用函数或使用 ...
- 【java】关键字volatile
volatile 1. 含义: volatile是JVM提供的轻量级的同步机制,具有三个特点:保证可见性.不保证原子性.禁止指令重排. 1.1 保证可见性 一个线程修改了共享变量并写回主内存,其他线程 ...
- python之robotframework+pycharm测试框架
一.robotframework简介 Robot Framework是一款python编写的功能自动化测试框架.具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进行分布式 ...
- [256个管理学理论]004.鲶鱼效应(Catfish Effect)
鲶鱼效应(Catfish Effect) 来自于大洋彼岸的让你看不懂的解释: 鲶鱼效应(Catfish Effect):鲶鱼在搅动小鱼生存环境的同时,也激活了小鱼的求生能力.鲶鱼效应是采取一种手段或措 ...
- 基于nodejs+express+mysql+webstorm+html的 增删改查
一.工具准备 Nodejs框架,WebStorm.Mysql服务.Navicat.此篇文章只讲项目的搭建过程,至于Nodejs,WebStorm.Mysql的下载.安装与配置网上资源很多,请自行查阅, ...
- golang内置类型和内置函数
golang内置类型和内置函数是不需要引入包直接可用的 golang内置类型: 数值类型 string int,unint float32,float64 bool array 有长度的 comple ...
- Rocket - devices - TLError
https://mp.weixin.qq.com/s/s_6qPkT2zwdqYLw5iK7_8g 简单介绍TLError的实现. 1. 继承自DevNullDevice TLError继承自DevN ...
- Rocket - tilelink - RegionReplicator
https://mp.weixin.qq.com/s/XZVCdt50tM6lavchGm9GRg 简单介绍RegionReplicator的实现. 1. 基本介绍 根据mask ...
- jchdl - GSL实例 - DLatch(D锁存器)
https://mp.weixin.qq.com/s/c8kDgye50nKJR4tkC0RzVA D锁存器对电平敏感,当使能位使能时,输出Q跟随输入D的变化而变化. 摘自康华光<电子 ...