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 * ...
随机推荐
- MyBatis-Generator最佳实践
引用地址:http://arccode.net/2015/02/07/MyBatis-Generator%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5/ 最近使用MyBati ...
- git 两个中心仓库上的分支 merge
首先在一个中心仓库里面添加另外一个仓库的所有分支. 命令: git remote add Cangku2 https://github.com/abc/abc.git git fetch 这之后在使用 ...
- 用jquery实现的QQ邮箱里的多收件人选取及其他效果改进版
我们先来看一下之前网上的版本效果: 发现很多bug且应用场景不一样,没办法只能自己写了 操作时 textarea 的值只是显示效果,实现的参数为 hidden 2017-04-25再次改进版与新版的 ...
- Work Management Service application in SharePoint 2016
最近开始弄SharePoint 2016的Workflow,遇到问题发现没有了Work Management Service application,然后用PowerShell命令创建也不行,bing ...
- PHP检测及判断手机登录用户是安卓或爱疯(iPhone)客户端
<?php /* PHP 自动判断客户端平台(PC.安卓.iPhone.平板) * strtolower() 函数把字符串转换为小写: * strpos() 函数返回字符串在另一个字符串中第一次 ...
- Java Run-Time Data Areas
前言 本文主要介绍JVM的运行时数据区 来自Oracle文档 Java Virtual Machine Specification -- Chapter 2. The Structure of the ...
- 为 hexo NexT 添加 Gitment 评论插件
Gitment 是作者imsun实现的一款基于 GitHub Issues 的评论系统. 支持在前端直接引入, 不需要任何后端代码. 可以在页面进行登录, 查看, 评论, 点赞等操作. 同时有完整的 ...
- 命令行修改mysql密码和远程访问
http://jingyan.baidu.com/article/a3a3f8118cea488da2eb8a0a.html
- tf.concat, tf.stack和tf.unstack的用法
tf.concat, tf.stack和tf.unstack的用法 tf.concat相当于numpy中的np.concatenate函数,用于将两个张量在某一个维度(axis)合并起来,例如: a ...
- 【中文分词系列】 4. 基于双向LSTM的seq2seq字标注
http://spaces.ac.cn/archives/3924/ 关于字标注法 上一篇文章谈到了分词的字标注法.要注意字标注法是很有潜力的,要不然它也不会在公开测试中取得最优的成绩了.在我看来,字 ...