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 ...
随机推荐
- [原创]Android中LocationManager的简单使用,获取当前位置
Android中LocationManager的提供了一系列方法来地理位置相关的问题,包括查询上一个已知位置:注册/注销来自某个 LocationProvider的周期性的位置更新:以及注册/注销接近 ...
- iOS开发:视图生命周期
iOS应用的视图状态分为以下几种 在viewcontroller的父类UIViewController中可以看到如下代码,通过重写不同的方法对操作视图渲染. @available(iOS 2.0, * ...
- unison + inotify 实现文件实时双向同步部署步骤
unison + inotify 实现文件实时双向同步部署步骤 一. Unison简介 Unison是Windows.Linux以及其他Unix平台下都可以使用的文件同步工具,它能使两个文件夹(本地或 ...
- Java [Leetcode 111]Minimum Depth of Binary Tree
题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
- ZOJ 1455 Schedule Problem(差分约束系统)
// 题目描述:一个项目被分成几个部分,每部分必须在连续的天数完成.也就是说,如果某部分需要3天才能完成,则必须花费连续的3天来完成它.对项目的这些部分工作中,有4种类型的约束:FAS, FAF, S ...
- 总结c++ primer中的notes
转载:http://blog.csdn.net/ace_fei/article/details/7386517 说明: C++ Primer, Fourth Edition (中英文)下载地址:htt ...
- Codeforces Round #361 (Div. 2) 套题
A - Mike and Cellphone 问有没有多解,每个点按照给出的序列用向量法跑一遍 #include<cstdio> #include<cstring> #incl ...
- Apache OFBiz 学习笔记 之 服务引擎 二
加载服务定义文件 ofbiz-component.xml:所有的服务定义文件在每个组件的ofbi-component.xml文件中 加载服务定义 例:framework/common/ofbi ...
- web自动化框架之四测试报告的搭建
现状: 看过前面的文章,楼主用的是python,所以在搭建测试报告这块的时候使用的是unittest+htmlTestRunner:然后发现生成出来的报告,总是有那么不完美的地方,比如想增加图片,比如 ...
- unicode ansi utf-8 unicode_big_endian编码的区别
随便说说字符集和编码 快下班时,爱问问题的小朋友Nico又问了一个问题: "sqlserver里面有char和nchar,那个n据说是指unicode的数据,这个是什么意思.&quo ...