在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

#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow> namespace Ui {
class MainWindow;
} //接收返回类型
typedef enum{
MB_EX_NONE = 0x00,
MB_EX_ILLEGAL_FUNCTION = 0x01,
MB_EX_ILLEGAL_DATA_ADDRESS = 0x02,
MB_EX_ILLEGAL_DATA_VALUE = 0x03,
MB_EX_SLAVE_DEVICE_FAILURE = 0x04,
MB_EX_ACKNOWLEDGE = 0x05,
MB_EX_SLAVE_BUSY = 0x06,
MB_EX_MEMORY_PARITY_ERROR = 0x08,
MB_EX_GATEWAY_PATH_FAILED = 0x0A,
MB_EX_GATEWAY_TGT_FAILED = 0x0B
} eMBException; class MainWindow : public QMainWindow
{
Q_OBJECT public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow(); typedef eMBException (MainWindow::*pxMBFunctionHandler )(); typedef struct
{
quint8 ucFunctionCode;
pxMBFunctionHandler pxHandler;
} xMBFunctionHandler; xMBFunctionHandler test[2]; eMBException fun1();
eMBException fun2(); private:
Ui::MainWindow *ui;
quint8 value;
}; #endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug> MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this); test[0].pxHandler = &MainWindow::fun1;
test[1].pxHandler = &MainWindow::fun2; (this->*test[0].pxHandler)();
(this->*test[1].pxHandler)();
} MainWindow::~MainWindow()
{
delete ui;
} eMBException MainWindow::fun1(){
value = 1;
qDebug("call fun1();");
return MB_EX_NONE;
} eMBException MainWindow::fun2(){
value = 2;
qDebug("call fun2();");
return MB_EX_NONE;
}

 

 

main.cpp

#include "mainwindow.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show(); return a.exec();
}

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. Awesome Javascript(中文翻译版)

    [导读]:GitHub 上有一个 Awesome – XXX 系列的资源整理.awesome-javascript 是 sorrycc 发起维护的 JS 资源列表,内容包括:包管理器.加载器.测试框架 ...

  2. Parallel WebDriver executions using TestNG

    In this post, we will see how does one make use of TestNG to kick off parallel UI tests using WebDri ...

  3. css,html命名规则

    css,html命名规则 页头: header 登录条: loginBar 标志: logo 侧栏: sideBar 广告: banner 导航: nav 子导航: subNav 菜单: menu 子 ...

  4. OK335xS psplash make-image-header.sh hacking

    /***************************************************************************** * OK335xS psplash mak ...

  5. Java [Leetcode 206]Reverse Linked List

    题目描述: Reverse a singly linked list. 解题思路: 使用递归或者迭代的方法. 代码如下: 方法一:递归 /** * Definition for singly-link ...

  6. poj 1087 A Plug for UNIX

    题目描述:现在由你负责布置Internet联合组织首席执行官就职新闻发布会的会议室.由于会议室修建时被设计成容纳全世界各地的新闻记者,因此会议室提供了多种电源插座用以满足(会议室修建时期)各国不同插头 ...

  7. Myeclipse中相同变量高亮显示

    不小心搞不显示了,解决: windows/MyEclipse-> preferences-> java-> Editor-> Mark Occurences 勾选即可

  8. 【经典DFS】NYOJ-1058-部分和问题

    [题目链接:NYOJ-1058] 看到题目难度是2,所以想也没想,直接循环比较...结果果然... 是错的. #include<cstdio> #include<cstring> ...

  9. 【转】《APUE》第三章笔记(4)及习题3-2

    原文网址:http://www.cnblogs.com/fusae-blog/p/4256794.html APUE第三章的最后面给出的函数,现在还用不着,所以,先留个名字,待到时候用着了再补上好了. ...

  10. 苹果iphone4s完美越狱后破解4g网络方法

    苹果iphone4s完美越狱后破解4g网络方法教程 作者:佚名 字体:[增加 减小] 来源:互联网 时间:01-15 10:07:25我要评论 自从港版iPhone5s/c能够破解移动4G网络后, i ...