这个概念主要用在C++中去实现"委托"的特性. 但现在C++11 中有了 更好用的function/bind 功能.但对于类的成员函数指针的概念我们还是应该掌握的.

类函数指针 就是要确定由哪个 类的实例 去调用 类函数指针所指的函数.

typedef void (Human::*fp)();  定义了一个类的函数指针.

fp classFunc = &Human::run; // 注意这里是方法的地址.告之具体的指向类中的哪个函数

(human->*p)(); 或 (human.*p)();  // 这是使用一个类的实例去调用类的函数指针 *p 就是取得方法的地址 然后再调用..

 #include <iostream>
using namespace std; class Human {
public:
virtual void run() = ;
virtual void eat() = ;
}; class Mother : public Human {
public:
void run() {
cout << "Mother Run" << endl;
}
void eat() {
cout << "Mother eat" << endl;
}
}; class Father : public Human {
public:
void run() {
cout << "Father run" << endl;
}
void eat() {
cout << "Father eat" << endl;
}
}; typedef void (Human::*fp)(); int main() {
// 创建一个类的实例
Human* human = new Father();
// 定义一个类函数指针
fp p;
// 给类函数指针赋值
p = &Human::run;
// 调用类的函数指针
(human->*p)();
return ;
}

C++ 类的成员函数指针 ( function/bind )的更多相关文章

  1. VB6/VBA中跟踪鼠标移出窗体控件事件(类模块成员函数指针CHooker类应用)

    一.关于起因 前几天发了一篇博文,是关于获取VB类模块成员函数指针的内容(http://www.cnblogs.com/alexywt/p/5880993.html):今天我就发一下我的应用实例. V ...

  2. C++ - 模板类模板成员函数(member function template)隐式处理(implicit)变化

    模板类模板成员函数(member function template)隐式处理(implicit)变化 本文地址: http://blog.csdn.net/caroline_wendy/articl ...

  3. 类的成员函数指针和mem_fun适配器的用法

    先来看一个最简单的函数: void foo(int a) { cout << a << endl; } 它的函数指针类型为 void (*)(int); 我们可以这样使用: v ...

  4. 获取VB类模块成员函数指针(转)

    最近在做一些VB6.VBA的项目,被如何获取类模块中的函数指针这个问题所困扰,收集整理后,有2分资料值得收藏,特将关键部分留存,以备后续查找. 参照连接1:http://www.cnblogs.com ...

  5. 类成员函数指针 ->*语法剖析

    在cocos2d-x中,经常会出现这样的调用,如 ->*,这个是什么意思呢,如下面得这个例子: , 其实这是对类的成员函数指针的调用,在cocos2dx中,这种形式多用于回调函数的调用.如我们经 ...

  6. 成员函数指针与高效C++委托 (delegate)

    下载实例源代码 - 18.5 Kb 下载开发包库文件 - 18.6 Kb 概要 很遗憾, C++ 标准中没能提供面向对象的函数指针. 面向对象的函数指针也被称为闭包(closures) 或委托(del ...

  7. 成员函数指针与高性能C++委托

    1 引子 标准C++中没有真正的面向对象的函数指针.这一点对C++来说是不幸的,因为面向对象的指针(也叫做“闭包(closure)”或“委托(delegate)”)在一些语言中已经证明了它宝贵的价值. ...

  8. [转]成员函数指针与高性能的C++委托

    原文(作者:Don Clugston):Member Function Pointers and the Fastest Possible C++ Delegates 译文(作者:周翔): 成员函数指 ...

  9. 从汇编看c++成员函数指针(二)

    下面先看一段c++源码: #include <cstdio> using namespace std; class X { public: virtual int get1() { ; } ...

随机推荐

  1. Partitioner没有被调用的情况

    map的输出,通过分区函数决定要发往哪个reducer. 有2种情况,我们自定义的Partitioner不会被调用 1) reducer个数为0 这种情况,没有reducer,不需要分区 2) red ...

  2. C/C++走过的坑(基础问题篇)

    1.有符号int与无符号int比较 #define TOTOL_ELEMENTS (sizeof(a) / sizeof(a[0]) ); int main() { int a[] = {23,24, ...

  3. iFreeThinking - 记录生活,分享思考

    http://www.ifreethinking.com iFreeThinking.com 是一个非营利性个人博客网站.开于 2014 年,博客主要记录分享一些思考和感悟. 文章列表:http:// ...

  4. 菜鸟学习Hibernate——缓存

    Hibernate的缓存分为三种:一级缓存.二级缓存.查询缓存.下面我就为大家介绍一下. 一.概念. 一级缓存:第一级存放于session中称为一级缓存.Session 级别的缓存,它同session ...

  5. knockoutjs表格增加更新删除

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...

  6. Mysql账号管理

    一 用户添加 通过insert 方式添加用户 insert into mysql.user(Host,User,Password) values("localhost"," ...

  7. poj 3580 SuperMemo

    题目连接 http://poj.org/problem?id=3580 SuperMemo Description Your friend, Jackson is invited to a TV sh ...

  8. go语言示例-Timer计时器的用法

    计时器用来定时执行任务,分享一段代码: package main import "time" import "fmt" func main() { //新建计时 ...

  9. Paragon NTFS for Mac免费获取官方赠送正版.更新获取ntfs for mac 14方法

    Paragon NTFS for Mac免费获取官方赠送正版,没有这个软件的朋友赶紧收下.获取地址http://www.paragon-drivers.com/cn/ntfs-mac-free/ntf ...

  10. ios8/sdk8/xcode6/iphone6(+)适配

    AppIcon https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/Ic ...