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 * ...
随机推荐
- 在ASP.NET MVC中实现一种不同于平常的三级联动、级联方式, 可用于城市、车型选择等多层级联场景
三级或多级联动的场景经常会碰到,比如省.市.区,比如品牌.车系.车型,比如类别的多级联动......我们首先想到的是用三个select来展示,这是最通常的做法.但在另外一些场景中,比如确定搜索条件的时 ...
- 解决xib布局方式支持ios6,ios7
xcode5 中的界面布局 根据sdk 分成ios7.0 and Later 和 ios6.1 and Earlier 两种,那如何xib同时支持 ios6 和ios7 的界面呢 方法如下: 在xco ...
- 安装GCC-4.6.1详细教程
一.什么是Gcc Linux系统下的Gcc(GNU C Compiler)是GNU推出的功能强大.性能优越的多平台编译器,是GNU的代表作品之一.gcc是可以在多种硬体平台上编译出可执行程序的超级编译 ...
- ibatis.net:第七天,QueryWithRowDelegate
xml <statement id="FindOrdersByCustomer" parameterClass="string" resultClass= ...
- 血族第四季/全集The Strain迅雷下载
当第四季开始时,故事时间已经过去九个月.世界陷入黑暗,斯特里高伊吸血鬼控制了一切.第三季结尾的爆炸引发了一场全球核灾难,核冬天的到来令地表变得暗无天日,斯特里高伊获得解放.它们大白天也能出来活动,帮助 ...
- FZU2169:shadow(最短路)
Problem Description YL是shadow国的国王,shadow国有N个城市.为了节省开支,shadow国仅仅有N-1条道路,这N-1条道路使得N个城市连通. 某一年,shadow国发 ...
- Mysql Case when 语句
首先我们创建一列sex.再为部分行设置好值0(女)或者1(男): 现在要做这样一件事,查询显示的时候sex不显示0,1和null,我们让它0的就显示女,1的就显示男,null就显示未知. 这时我们 ...
- HTTP Error 502.5 – Process Failure
https://www.cnblogs.com/lookerblue/p/7101641.html http://www.cnblogs.com/lookerblue/p/7102040.html h ...
- TCP/IP协议体系结构简介
1.TCP/IP协议栈 四层模型 TCP/IP这个协议遵守一个四层的模型概念:应用层.传输层.互联层和网络接口层. 网络接口层:模型的基层是网络接口层.负责数据帧的发送和接收,帧是独立的网络信息传输单 ...
- iOS:UICollectionView的扩展应用
一.介绍 CollectionView是iOS中一个非常重要的控件,它可以实现很多的炫酷的效果,例如轮播图.瀑布流.相册浏览等.其实它和TableView很相似,都是对cell进行复用,提高系统性能. ...