c/c++ 函数指针的用法
【目录】
c语言函数指针的定义形式:返回类型 (*函数指针名称)(参数类型,参数类型,参数类型,…);
c++函数指针的定义形式:返回类型 (类名称::*函数成员名称)(参数类型,参数类型,参数类型,….);
以下代码编译环境:codeblocks with gcc in win 7
c语言函数指针使用举例:
#include <stdio.h>
#include <stdlib.h> int fun1()
{
printf("this is fun1 call\n");
return ;
} void fun2(int k, char c)
{
printf("this is fun2 call:%d %c\n", k, c);
} int main()
{
int (*pfun1)() = NULL;
void (*pfun2)(int, char) = NULL;
int a,b;
pfun1 = fun1; //第一种赋值方法
a = pfun1(); //第一种调用方法(推荐)
printf("%d\n",a);
b = (*pfun1)();//第二种调用方法
printf("%d\n",b);
pfun2 = &fun2;//第二种赋值方法(推荐,因为和其他数据指针赋值方法一致)
pfun2(,'a');
(*pfun2)(,'b');
return ;
}
c++函数指针使用举例:
#include <iostream>
using namespace std; class test
{
public:
test()
{
cout<<"constructor"<<endl;
}
int fun1(int a, char c)
{
cout<<"this is fun1 call:"<<a<<" "<<c<<endl;
return a;
}
void fun2(double d)const
{
cout<<"this is fun2 call:"<<d<<endl;
}
static double fun3(char buf[])
{
cout<<"this is fun3 call:"<<buf<<endl;
return 3.14;
}
}; int main()
{
// 类的静态成员函数指针和c的指针的用法相同
double (*pstatic)(char buf[]) = NULL;//不需要加类名
pstatic = test::fun3; //可以不加取地址符号
pstatic("myclaa");
pstatic = &test::fun3;
(*pstatic)("xyz"); //普通成员函数
int (test::*pfun)(int, char) = NULL; //一定要加类名
pfun = &test::fun1; //一定要加取地址符号
test mytest;
(mytest.*pfun)(, 'a'); //调用是一定要加类的对象名和*符号 //const 函数(基本普通成员函数相同)
void (test::*pconst)(double)const = NULL; //一定要加const
pconst = &test::fun2;
test mytest2;
(mytest2.*pconst)(3.33); // //构造函数或者析构函数的指针,貌似不可以,不知道c++标准有没有规定不能有指向这两者的函数指针
// (test::*pcon)() = NULL;
// pcon = &test.test;
// test mytest3;
// (mytest3.*pcon)(); return ;
}
函数指针作为函数参数:
#include <stdio.h>
#include <stdlib.h> void fun(int k, char c)
{
printf("this is fun2 call:%d %c\n", k, c);
} void fun1(void (*pfun)(int, char), int a, char c)
{
pfun(a, c);
} int main()
{
fun1(fun, , 'a');
return ;
}
// c++ 的形式差不多
函数指针作为函数返回值:
// c 形式
#include <stdio.h>
#include <stdlib.h> void fun(int k, char c)
{
printf("this is fun2 call:%d %c\n", k, c);
} //fun1 函数的参数为double,返回值为函数指针void(*)(int, char)
void (*fun1(double d))(int, char)
{
printf("%f\n",d);
return fun;
} int main()
{
void (*p)(int, char) = fun1(3.33);
p(, 'a');
return ;
}
//c++ 形式
#include <iostream>
using namespace std; class test
{
public:
int fun(int a, char c)
{
cout<<"this is fun call:"<<a<<" "<<c<<endl;
return a;
}
}; class test2
{
public:
// test2 的成员函数fun1,参数是double,
//返回值是test的成员函数指针int(test::*)(int, char)
int (test::*fun1(double d))(int, char)
{
cout<<d<<endl;
return &test::fun;
}
}; int main()
{
test mytest;
test2 mytest2;
int (test::*p)(int, char) = mytest2.fun1(3.33);
(mytest.*p)(, 'a');
return ;
}
函数指针数组:
#include <stdio.h>
#include <stdlib.h> float add(float a,float b){return a+b;}
float minu(float a,float b){return a-b;} int main()
{
//定义一个函数指针数组,大小为2
//里面存放float (*)(float, float)类型的指针
float (*pfunArry[])(float, float) = {&add, &minu};
double k = pfunArry[](3.33,2.22);// 调用
printf("%f\n", k);
k = pfunArry[](3.33,2.22);
printf("%f\n", k);
return ;
}
//c++ 可类比
typedef 简化函数指针类型:
#include <stdio.h>
#include <stdlib.h> float add(float a,float b)
{
printf("%f\n",a+b);
return a+b;
}
float minu(float a,float b)
{
printf("%f\n",a-b);
return a-b;
} //用pfunType 来表示float(*)(float, float)
typedef float(*pfunType)(float, float); int main()
{
pfunType p = &add;//定义函数指针变量
p(3.33, 2.22);
pfunType parry[] = {&add, &minu};//定义函数指针数组
parry[](3.33, 2.22);
//函数指针作为参数可以定义为:void fun(pfunType p)
//函数指针作为返回值可以定义为:pfunType fun(); return ;
}
//c++ 可类比
【版权声明】转载请注明出处 http://www.cnblogs.com/TenosDoIt/p/3164081.html
c/c++ 函数指针的用法的更多相关文章
- (转)typedef 函数指针的用法
typedef 函数指针的用法 在网上搜索函数指针,看到一个例子.开始没看懂,想放弃,可是转念一想,这个用法迟早要弄懂的,现在多花点时间看懂它,好过以后碰到了要再花一倍时间来弄懂它.其实很多时候都 ...
- typedef 函数指针的用法
转自:http://www.cnblogs.com/shenlian/archive/2011/05/21/2053149.html typedef 函数指针的用法 在网上搜索函数指针,看到一个例子. ...
- IOS typedef 函数指针的用法
代码简化, 促进跨平台开发的目的. typedef 行为有点像 #define 宏,用其实际类型替代同义字. 不同点:typedef 在编译时被解释,因此让编译器来应付超越预处理器能力的文本替换. 用 ...
- C函数指针的用法
1.最简单的用法: #include <cstdio> int (*p)(int);//定义一个函数指针变量p(下面的f其实是一个常量函数指针) int f(int x) { printf ...
- C++成员函数指针错误用法警示(成员函数指针与高性能的C++委托,三篇),附好多评论
今天做一个成绩管理系统的并发引擎,用Qt做的,仿照QtConcurrent搞了个模板基类.这里为了隐藏细节,隔离变化,把并发的东西全部包含在模板基类中.子类只需注册需要并发执行的入口函数即可在单独线程 ...
- C语言函数指针的用法
函数指针是一种在C.C++.D语言.其他类 C 语言和Fortran 2003中的指针.函数指针可以像一般函数一样,用于调用函数.传递参数.在如 C 这样的语言中,通过提供一个简单的选取.执行函数的方 ...
- C++ 指向类成员函数指针的用法(转自维基百科)
类成员函数指针 类成员函数指针(member function pointer),是C++语言的一类指针数据类型,用于存储一个指定类具有给定的形参列表与返回值类型的成员函数的访问信息. 目录 1 语法 ...
- C中的函数指针的用法
include<stdio.h> typedef int (*Cal)(int a,int b);//定义一个函数指针,第一个int是指向函数的返回值的类型,后面括号里面的两个int是指指 ...
- x64系统的判断和x64下文件和注册表访问的重定向(举例了GetProcAddress后转成函数指针的用法)
判断一个32位应用程序是否运行在x64系统下,可以使用下面的代码: BOOL IsX64System() { BOOL bIsWow64 = FALSE; typedef BOOL (WINAPI * ...
随机推荐
- 为什么使用this构造器
当一个类有多个构造函数的时候,常使用this构造器: public class SomeClass { public SomeClass() { //TODO:初始化一些字段 } public Som ...
- (Delphi) Using the Disk Cache 使用磁盘缓存
The Chilkat Spider component has disk caching capabilities. To setup a disk cache, create a new dire ...
- Ora-01536:超出了表空间users的空间限量
正在开会,同事跑过来说数据库有问题,通讯程序不能入库,赶快获取一条insert into a values()语句后在toad工具中手动插入,发现报错:Ora-01536:超出了表空间users的 ...
- blob转base64位 base64位转blob
//**dataURL to blob** function dataURLtoBlob(dataurl) { var arr = dataurl.split(','), mime = arr[0]. ...
- jQuery Ajax 上传文件改进
如果用户取消上传后 背景 提示自动消失了.... 修正Bug.... 同时也更新了不同上传类型的提示字体大小... 2017-05-26 增加了鼠标释放提示 先看之前的效果: 再看现在的效果: 升级 ...
- java高分局之jstat命令使用(转)
转自:http://blog.csdn.net/h_025/article/details/52813817 java高分局之jstat命令使用 jstat命令可以查看堆内存各部分的使用量,以及加载类 ...
- 同志亦凡人第五季/全集BQueer As Folk 5迅雷下载
同志亦凡人 第五季 Queer as Folk Season 5 (2005) 本季看点:这是一群生活在匹兹堡男人和男人,女人和女人的故事.在他们的王国里有各色人物.王国的国王Brian(葛尔?哈罗德 ...
- Java并发编程的艺术(八)——闭锁、同步屏障、信号量详解
1. 闭锁:CountDownLatch 1.1 使用场景 若有多条线程,其中一条线程需要等到其他所有线程准备完所需的资源后才能运行,这样的情况可以使用闭锁. 1.2 代码实现 // 初始化闭锁,并设 ...
- bash 设置
export COLUMNS=500 $* 将所有命令行参数当做单个文本值包含 $@ 将所有命令行参数当做独立的文本值包含 $# 命令行参数数目 $? 最近使用的前端进程的退出状态码 $- 当前命令选 ...
- java通过Stream对list集合分组
java通过Stream对list集合分组 现在有一个List集合,想对该集合中的数据分组处理,想到java8中的stream,就搞来试试,非常给力!例子如下 1 2 3 4 5 6 7 8 9 10 ...