在C++类中使用函数指针。

类型定义:
     typedef 返回类型(类名::*新类型)(参数表)

//类定义

class CA

{

public:

char lcFun(int a) { return; }

};

     CA ca;
     typedef char (CA::*PTRFUN)(int);
     PTRFUN pFun;
    void main()
    {
       pFun = CA::lcFun;
       ca.(*pFun)(2);
    }
       

在这里,指针的定义与使用都加上了“类限制”或“对象”,用来指明指针指向的函数是那个类的这里的类对象也可以是使用new得到的。比如:
     CA *pca = new CA;
     pca->(*pFun)(2);
     delete pca;
     而且这个类对象指针可以是类内部成员变量,你甚至可以使用this指针。比如:类CA有成员变量PTRFUN m_pfun;

     void CA::lcFun2()
     {
         (this->*m_pFun)(2);
     }

 

一句话,使用类成员函数指针必须有“->*”或“.*”的调用。

在Qt下的demo:

 

MainWindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5.  
  6. namespace Ui {
  7. class MainWindow;
  8. }
  9.  
  10. //接收返回类型
  11. typedef enum{
  12. MB_EX_NONE = 0x00,
  13. MB_EX_ILLEGAL_FUNCTION = 0x01,
  14. MB_EX_ILLEGAL_DATA_ADDRESS = 0x02,
  15. MB_EX_ILLEGAL_DATA_VALUE = 0x03,
  16. MB_EX_SLAVE_DEVICE_FAILURE = 0x04,
  17. MB_EX_ACKNOWLEDGE = 0x05,
  18. MB_EX_SLAVE_BUSY = 0x06,
  19. MB_EX_MEMORY_PARITY_ERROR = 0x08,
  20. MB_EX_GATEWAY_PATH_FAILED = 0x0A,
  21. MB_EX_GATEWAY_TGT_FAILED = 0x0B
  22. } eMBException;
  23.  
  24. class MainWindow : public QMainWindow
  25. {
  26. Q_OBJECT
  27.  
  28. public:
  29. explicit MainWindow(QWidget *parent = 0);
  30. ~MainWindow();
  31.  
  32. typedef eMBException (MainWindow::*pxMBFunctionHandler )();
  33.  
  34. typedef struct
  35. {
  36. quint8 ucFunctionCode;
  37. pxMBFunctionHandler pxHandler;
  38. } xMBFunctionHandler;
  39.  
  40. xMBFunctionHandler test[2];
  41.  
  42. eMBException fun1();
  43. eMBException fun2();
  44.  
  45. private:
  46. Ui::MainWindow *ui;
  47. quint8 value;
  48. };
  49.  
  50. #endif // MAINWINDOW_H

mainwindow.cpp

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QDebug>
  4.  
  5. MainWindow::MainWindow(QWidget *parent) :
  6. QMainWindow(parent),
  7. ui(new Ui::MainWindow)
  8. {
  9. ui->setupUi(this);
  10.  
  11. test[0].pxHandler = &MainWindow::fun1;
  12. test[1].pxHandler = &MainWindow::fun2;
  13.  
  14. (this->*test[0].pxHandler)();
  15. (this->*test[1].pxHandler)();
  16. }
  17.  
  18. MainWindow::~MainWindow()
  19. {
  20. delete ui;
  21. }
  22.  
  23. eMBException MainWindow::fun1(){
  24. value = 1;
  25. qDebug("call fun1();");
  26. return MB_EX_NONE;
  27. }
  28.  
  29. eMBException MainWindow::fun2(){
  30. value = 2;
  31. qDebug("call fun2();");
  32. return MB_EX_NONE;
  33. }

 

 

main.cpp

  1. #include "mainwindow.h"
  2. #include <QApplication>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. QApplication a(argc, argv);
  7. MainWindow w;
  8. w.show();
  9.  
  10. return a.exec();
  11. }

C++中使用函数指针 【瓦特芯笔记】的更多相关文章

  1. QT中使用函数指针

    想仿命令行,所以定义了一个类,让一个String 对应一个 function,将两者输入list容器. 类中定义了 QString commandStr; void (MainWindow::*com ...

  2. Delphi中的函数指针判断是否为空

    delphi函数指针 只有@@p才代表了函数指针本身的地址   assigned(p) 判断是否为空 或者用 @p=nil 来判断函数指针是不是为空 Delphi中的函数指针实际上就是指针,只是在使用 ...

  3. 1、C语言中的函数指针

    一 通常的函数调用 void MyFun(int x); //此处的申明也可写成:void MyFun( int ); int main(int argc, char* argv[]) { MyFun ...

  4. C语言结构体中的函数指针

      这篇文章简单的叙述一下函数指针在结构体中的应用,为后面的一系列文章打下基础 本文地址:http://www.cnblogs.com/archimedes/p/function-pointer-in ...

  5. Keil C51 中的函数指针和再入函数

    函数指针是C语言中几个难点之一.由于8051的C编译器的独特要求,函数指针和再入函数有更多的挑战需要克服.主要由于函数变量的传递.典型的(绝大部分8051芯片)函数变量通过堆栈的入栈和出栈命令来传递. ...

  6. C++中的函数指针和指针函数

    C++中的函数指针和指针函数       数组名一般可以被当成指向数组第一个元素的常量指针,同样的情况,在函数中,函数名可以本当成指向函数的常量指针,假如一个函数已经定义,那么它在计算机中一定有特定的 ...

  7. C语言中的函数指针

    C语言中的函数指针 函数指针的概念:   函数指针是一个指向位于代码段的函数代码的指针. 函数指针的使用: #include<stdio.h> typedef struct (*fun_t ...

  8. 利用C语言中的函数指针实现c++中的虚函数

    C语言中的函数指针 #include<stdio.h> int fun1(int a) { return a*a; } int fun2(int a) { return a*a*a; } ...

  9. C/C++中的函数指针

    C/C++中的函数指针 一.引子 今天无聊刷了leetcode上的一道题,如下: Median is the middle value in an ordered integer list. If t ...

随机推荐

  1. Web内容管理系统 Magnolia 介绍-挖掘优良的架构(1)

    Magnolia简介: Magnolia CMS是一家瑞士公司自2003年起发布的一个基于Java的开源内容管理系统.它适合且已被使用在以下领域:电子商务(例如:COOP.Migros.Rossman ...

  2. 函数lock_rec_get_first

    /*********************************************************************//** Gets the first explicit l ...

  3. UVa 557 (概率 递推) Burger

    题意: 有两种汉堡给2n个孩子吃,每个孩子在吃之前要抛硬币决定吃哪一种汉堡.如果只剩一种汉堡,就不用抛硬币了. 求最后两个孩子吃到同一种汉堡的概率. 分析: 可以从反面思考,求最后两个孩子吃到不同汉堡 ...

  4. Apache Struts2 s2-020补丁安全绕过漏洞

    CNVD-ID CNVD-2014-01552 发布时间 2014-03-11 危害级别 高 影响产品 Apache struts 2.0.0-2.3.16 BUGTRAQ ID 65999 CVE ...

  5. C扩展Python - official docs - defining new type

    1. Code & Official_doc: THIS 2. My question. #include <Python.h> /* * 1.PyTypeObject doc, ...

  6. Discuz!NT静态文件缓存(SQUID)

    在目前最新版本的产品中,我们提供了缓存静态文件的解决方案,就是使用SQUID做静态前端,将论坛中的大部分静态文件布署或外链到一个新的HTTP链接上,其中可以外链的静态文件包括:      1.Disc ...

  7. wait函数返回值总结

    之前在学习wait和waitpid函数的时候,就对使用宏WIFEXITED来检查获取的进程终止状态产生过疑惑:一般我们在程序中是调用的exit或者_exit函数来退出的,那么wait和waitpid函 ...

  8. 修改eOS wingpanel的透明度与颜色

    打开终端,输入: sudo scratch-text-editor /usr/share/themes/elementary/gtk-3.0/apps.css 修改.panel与.panel-shad ...

  9. codeforces 681D Gifts by the List dfs+构造

    题意:给你一个森林,表示其祖先关系(自己也是自己的祖先),每个人有一个礼物(要送给这个人的固定的一个祖先) 让你构造一个序列,使得的对于每个人,这个序列中第一个出现的他的祖先,是他要送礼物的的那个祖先 ...

  10. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 page_should_contain_list(self, locator, message='', loglevel='INFO')

    def page_should_contain_list(self, locator, message='', loglevel='INFO'): """Verifies ...