C Pointer-to-Function 与 C++ Pointer-to-MemberFunction 的区别
在看APUE Figure1.10的时候发现signal(SIGINT, sig_int)这里的sig_int直接用的函数名,但是看Thinking-in-C++ Vol.2的时候发现mem_fun(&Shape::draw)却对函数名进行了取地址操作,感觉有疑问就查了一下资料,下面的代码可以展示出这两者之间的一些区别
参考资料:
http://stackoverflow.com/questions/3050805/pointer-to-const-member-function-typedef
http://www.cplusplus.com/reference/functional/mem_fun1_t/
http://www.cnblogs.com/taobataoma/archive/2007/08/30/875743.html
http://www.cplusplus.com/reference/functional/mem_fun/
代码:
#include <functional>
#include <iostream>
#include <algorithm> using namespace std; void my(int arg); class MyClass
{
public:
void my(int arg) { cout << arg << endl; }
}; // 方法1,2 typedef void func_ptr(int); // func_ptr与func_ptr2本质是一样的,选择哪种定义方式看你的喜好
typedef void (*func_ptr2)(int); // 参考:http://www.cnblogs.com/qrlozte/p/4439002.html
void dosomething_one(func_ptr ptr);
void dosomething_two(func_ptr2 ptr); // 方法3,4,5 void dosomething_three(void (MyClass::*my_ptr)(int)); typedef void FuncType(int);
typedef FuncType MyClass::*MyClassFuncType; typedef void (MyClass::*MemberFuncType)(int); void dosomething_four(MyClassFuncType ptr); void dosomething_five(MemberFuncType ptr); int main() {
/*
方法1,2本质是一样的
方法3,4,5本质也是一样的
*/
dosomething_one(my);
dosomething_two(my);
dosomething_three(&MyClass::my);
dosomething_four(&MyClass::my);
dosomething_five(&MyClass::my);
return ;
} ///:~ void my(int arg)
{
cout << arg << endl;
} void dosomething_one(func_ptr ptr)
{
ptr();
} void dosomething_two(func_ptr ptr)
{
ptr();
} void dosomething_three(void (MyClass::*my_ptr)(int))
{
MyClass *obj = new MyClass;
(obj->*my_ptr)();
delete obj;
} void dosomething_four(MyClassFuncType ptr)
{
MyClass *obj = new MyClass;
(obj->*ptr)();
delete obj;
} void dosomething_five(MemberFuncType ptr)
{
MyClass *obj = new MyClass;
(obj->*ptr)();
delete obj;
}
C Pointer-to-Function 与 C++ Pointer-to-MemberFunction 的区别的更多相关文章
- pointer to function
指针.函数.数字.结构体.指针函数.函数指针 初学不好区分,做点儿实验来有效区分一下,以下代码采用dev-C++平台测试 //pointer to fucntion 函数功能是 基地址加偏移量得到偏移 ...
- 函数指针(pointer to function)——qsort函数应用实例
一,举例应用 在ACM比赛中常使用 stdlib.h 中自带的 qsort 函数,是教科书式的函数指针应用示范. #include <stdio.h> #include <stdli ...
- 指针函数(Pointer Function)和函数指针(Pointer to Function或Function Pointer)
一.指针函数 1.解释:指针函数很好理解:简单来说,就是一个返回指针的函数,本质是一个函数.如: int fun(int x,int y); //这是一个普通函数的声明,返回值是一个int类型, ...
- 野指针(Wild pointer)和悬垂指针(dangling pointer)
详细参考如下: Dangling pointer(悬垂指针.迷途指针)和 Wild pointer(野指针) 迷途指针经常出现在混杂使用malloc() 和 free() 库调用: 当指针指向的内存释 ...
- [C++] the pointer array & the array's pointer
int *p[4]------p是一个指针数组,每一个指向一个int型的int (*q)[4]---------q是一个指针,指向int[4]的数组 --> type: int(*)[4] vo ...
- js中 var functionName = function() {} 和 function functionName() {} 两种函数声明的区别
js中有两种声明函数的方法,分别为: var functionOne = function() { // Some code }; function functionTwo() { // Some c ...
- js中 var functionName = function() {} 和 function functionName() {} 两种函数声明的区别 (译)
stackOverflow中看到了很久以前问的一个关于函数声明的问题,问题对函数剖析的特别深.这里翻译了一下组织成一篇小博文,加深一下对这两种声明方式的印象.虽是老调重弹,但是只要能帮助理解问题,不管 ...
- JS中var声明与function声明以及构造函数声明方式的区别
JS中常见的三种函数声明(statement)方式有这三种: // 函数表达式(function expression) var h = function () { // h } // 函数声明(fu ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- .NET软件工程师面试总结
1.手写画出系统架构图,系统代码架构,有什么技术难点? 2.手写画出系统部署图 CDN(一般购买别人的服务器会自动CDN,他们自己配置就OK啦) 3.asp.net 的session怎么实现会话共享 ...
随机推荐
- 关闭使用ShellExecute打开的进程!!!!!
前言: 最近做一个项目使用到ShellExecute来打开一个带参数的外部exe文件,关闭时遇到不少问题,最终解决,总结如下. 对于关闭ShellExecute打开的进程窗口,网上比较多的是用Find ...
- 【补间动画示例】Tweened Animation
代码中定义动画示例 public class MainActivity extends ListActivity </integer> 常用的Activity转场动画中的补间动画 publ ...
- 阿里云 Ubuntu 14.04 安装mysql 5.6
1. 升级apt-get apt-get update 如果出现 : 说明没有你不是root用户,则需要用sudo命令 sudo apt-get update 下面出现权限问题都可以参照这个方法. 2 ...
- 更改DNS轻松访问google.com,FaceBook,Youtube等
将默认的Dns更改为42.120.21.30即可打开 https://www.google.com/ https://www.facebook.com/ https://www.youtube.com ...
- FrameWork中SQLServer数据源使用宏函数出错解决办法
环境:DW是SQLServer2008 建模工具:Cognos FrameWork Version:Cognos10.2 出现问题:在FM中创建查询主题,sql类型数据源,引入宏函数#CAMIDLis ...
- 近200篇机器学习&深度学习资料分享(含各种文档,视频,源码等)(1)
原文:http://developer.51cto.com/art/201501/464174.htm 编者按:本文收集了百来篇关于机器学习和深度学习的资料,含各种文档,视频,源码等.而且原文也会不定 ...
- 【精】iOS6 及其以上版本号自己主动旋转、手动强制旋转方案及布局适配
1.布局适配方式 本文不讨论哪种布局适配方式最好.此处使用的是 Masonry 纯代码布局适配. (Masonry 底层就是 AutoLayout 的 NSLayoutConstraint) 2.iO ...
- Exdata cell 节点配置时遇到的一个问题
问题描写叙述: [celladmin@vrh4 ~]$ cellcli CellCLI: Release 11.2.3.2.0 - Production on Sat Jun 14 09:11:08 ...
- Unity 事件2
UIMouseEvent.cs: using UnityEngine; using System; public abstract class UIMouseEvent : MonoBehaviour ...
- CA证书服务器从2003迁移到2008 R2!
1.在源 CA 服务器中备份相应的 CA :