C++中使用函数指针 【瓦特芯笔记】
在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++中使用函数指针 【瓦特芯笔记】的更多相关文章
- QT中使用函数指针
想仿命令行,所以定义了一个类,让一个String 对应一个 function,将两者输入list容器. 类中定义了 QString commandStr; void (MainWindow::*com ...
- Delphi中的函数指针判断是否为空
delphi函数指针 只有@@p才代表了函数指针本身的地址 assigned(p) 判断是否为空 或者用 @p=nil 来判断函数指针是不是为空 Delphi中的函数指针实际上就是指针,只是在使用 ...
- 1、C语言中的函数指针
一 通常的函数调用 void MyFun(int x); //此处的申明也可写成:void MyFun( int ); int main(int argc, char* argv[]) { MyFun ...
- C语言结构体中的函数指针
这篇文章简单的叙述一下函数指针在结构体中的应用,为后面的一系列文章打下基础 本文地址:http://www.cnblogs.com/archimedes/p/function-pointer-in ...
- Keil C51 中的函数指针和再入函数
函数指针是C语言中几个难点之一.由于8051的C编译器的独特要求,函数指针和再入函数有更多的挑战需要克服.主要由于函数变量的传递.典型的(绝大部分8051芯片)函数变量通过堆栈的入栈和出栈命令来传递. ...
- C++中的函数指针和指针函数
C++中的函数指针和指针函数 数组名一般可以被当成指向数组第一个元素的常量指针,同样的情况,在函数中,函数名可以本当成指向函数的常量指针,假如一个函数已经定义,那么它在计算机中一定有特定的 ...
- C语言中的函数指针
C语言中的函数指针 函数指针的概念: 函数指针是一个指向位于代码段的函数代码的指针. 函数指针的使用: #include<stdio.h> typedef struct (*fun_t ...
- 利用C语言中的函数指针实现c++中的虚函数
C语言中的函数指针 #include<stdio.h> int fun1(int a) { return a*a; } int fun2(int a) { return a*a*a; } ...
- C/C++中的函数指针
C/C++中的函数指针 一.引子 今天无聊刷了leetcode上的一道题,如下: Median is the middle value in an ordered integer list. If t ...
随机推荐
- Web内容管理系统 Magnolia 介绍-挖掘优良的架构(1)
Magnolia简介: Magnolia CMS是一家瑞士公司自2003年起发布的一个基于Java的开源内容管理系统.它适合且已被使用在以下领域:电子商务(例如:COOP.Migros.Rossman ...
- 函数lock_rec_get_first
/*********************************************************************//** Gets the first explicit l ...
- UVa 557 (概率 递推) Burger
题意: 有两种汉堡给2n个孩子吃,每个孩子在吃之前要抛硬币决定吃哪一种汉堡.如果只剩一种汉堡,就不用抛硬币了. 求最后两个孩子吃到同一种汉堡的概率. 分析: 可以从反面思考,求最后两个孩子吃到不同汉堡 ...
- Apache Struts2 s2-020补丁安全绕过漏洞
CNVD-ID CNVD-2014-01552 发布时间 2014-03-11 危害级别 高 影响产品 Apache struts 2.0.0-2.3.16 BUGTRAQ ID 65999 CVE ...
- C扩展Python - official docs - defining new type
1. Code & Official_doc: THIS 2. My question. #include <Python.h> /* * 1.PyTypeObject doc, ...
- Discuz!NT静态文件缓存(SQUID)
在目前最新版本的产品中,我们提供了缓存静态文件的解决方案,就是使用SQUID做静态前端,将论坛中的大部分静态文件布署或外链到一个新的HTTP链接上,其中可以外链的静态文件包括: 1.Disc ...
- wait函数返回值总结
之前在学习wait和waitpid函数的时候,就对使用宏WIFEXITED来检查获取的进程终止状态产生过疑惑:一般我们在程序中是调用的exit或者_exit函数来退出的,那么wait和waitpid函 ...
- 修改eOS wingpanel的透明度与颜色
打开终端,输入: sudo scratch-text-editor /usr/share/themes/elementary/gtk-3.0/apps.css 修改.panel与.panel-shad ...
- codeforces 681D Gifts by the List dfs+构造
题意:给你一个森林,表示其祖先关系(自己也是自己的祖先),每个人有一个礼物(要送给这个人的固定的一个祖先) 让你构造一个序列,使得的对于每个人,这个序列中第一个出现的他的祖先,是他要送礼物的的那个祖先 ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 page_should_contain_list(self, locator, message='', loglevel='INFO')
def page_should_contain_list(self, locator, message='', loglevel='INFO'): """Verifies ...