类里面的成员函数指针使用
先看代码
#include <iostream>
using namespace std;typedef void (*func)(); //含简单的定义一个函数指针,很容易用这个方法来实现指令,用作各种handler等等void print()
{cout << "print" <<endl;
}class A
{typedef void (A::*classfunp)();//指明这个函数指针是指向类A的,不能指向其他类的函数public:classfunp g;void print(){cout << "A::print" << endl;}void h(){g = &A::print; // 将函数print在A中的地址赋给函数指针g。使用&this->print报错为forbids taking the address of a bound memeber function to from a pointer to memeber function,意思就是不能将一个成员函数的地址当成一个指向成员函数的指针,下面解释,为什么(this->*g)(); //去掉this不对,因为g并不是A的一个成员变量,报错为:must use .* or->* to call point-to-member function}};
int main()
{ A a; a.h(); returnr 0;
}
----->A::print;
要知道为什么不能用this->print,请看
4.2Array-to-pointer conversion [conv.array]
1An lvalue or rvalue of type “array ofN T” or “array of unknownbound of T” can be converted to an rvalue
oftype “pointer to T.” The result is a pointer to the first elementof the array.
2 A string literal(2.13.4) that is not a wide string literal can be converted to anrvalue of type “pointer to
char”; awide string literal can be converted to an rvalue of type “pointerto wchar_t”. In either case,
theresult is a pointer to the first element of the array. Thisconversion is considered only when there is an
explicitappropriate pointer target type, and not when there is a general needto convert from an lvalue to an
rvalue.[Note: this conversion is deprecated. See Annex D. ] For the purposeof ranking in overload resolution
(13.3.3.1.1),this conversion is considered an array-to-pointer conversion followedby a qualification
conversion (4.4).[Example: "abc" is converted to “pointer to const char”as an array-to-pointer conversion,
andthen to “pointer to char” as a qualification conversion. ]
4.3Function-to-pointer conversion [conv.func]
1An lvalue of function type T can be converted to an rvalue of type“pointer to T.” The result is a pointer to
thefunction.50)
2 [Note: See 13.4 foradditional rules for the case where the function is overloaded.]
从上面我们知道为什么不能直接使用一个对象的函数地址了,因为重载,例如
一个类里面有函数f(),f(int),当我们使用this->f时,到底使用的是要哪个f的地址呢,除非我们传入参数进去
通过下面的例子就知道了
typedef void (*func)();
void f();
void f(int);func p = f;
p();
p(1);//错误,因为函数指针p是指向f()的而不是f(1)
通过使用函数指针,我们可以制作各种handler,用这些handler为我们做不同的事,如信号的一个函数
signal(intsigno, int (*handler)());
在游戏里面编写各种gm指令;做一些框架,为通用功能的特殊部分编写自己的接口,通过函数指着指向他们等等
类里面的成员函数指针使用相关推荐
- 类的成员函数指针和静态成员函数指针 调用成员函数***
C++提供static这个关键词对静态成员进行声明,静态成员函数和类的实例化无关,对于同一类来说,静态成员函数是所有类的对象共享的.而普通成员函数需要实例化才能调用,对于每一个实例来说,普通成员函数是 ...
- 成员函数指针与高性能的C++委托
成员函数指针与高性能的C++委托 http://www.cnblogs.com/jans2002/archive/2006/10/13/528160.html Member Function Poin ...
- 成员函数指针与高性能的C++委托(上篇)
成员函数指针与高性能的C++委托(上篇) 撰文:Don Clugston 引子 标准C++中没有真正的面向对象的函数指针.这一点对C++来说是不幸的,因为面向对象的指针(也叫做"闭包(clo ...
- 成员函数指针与高性能的C++委托(中篇)
成员函数指针与高性能的C++委托(中篇) 撰文:Don Clugston (接上篇) 成员函数指针--为什么那么复杂? 类的成员函数和标准的C函数有一些不同.与被显式声明的参数相似,类的成员函数有一个 ...
- 成员函数指针与高性能的C++委托 (Member Function Pointers and the Fastest Possible C++ Delegates)...
标准C++中没有真正的面向对象的函数指针.这一点对C++来说是不幸的,因为面向对象的指针(也叫做"闭包(closure)"或"委托(delegate)")在一些 ...
- 类的成员函数指针和mem_fun适配器的用法
先来看一个最简单的函数: void foo(int a) {cout << a << endl; } 它的函数指针类型为 void (*)(int); 我们可以这样使用: vo ...
- 类成员函数指针的语法
/*类成员函数指针的语法*/ /*****************************类.h文件************************************/ #if !defined ...
- C++函数指针和类成员函数指针
一.函数指针--基本形式 char Fun(int n) { return char(n); } //char(*pFun)(int);void main() {char(*pFun)(int);pF ...
- C++ 类的成员函数指针 ( function/bind )
这个概念主要用在C++中去实现"委托"的特性. 但现在C++11 中有了 更好用的function/bind 功能.但对于类的成员函数指针的概念我们还是应该掌握的. 类函数指针 就 ...
最新文章
- [logstash-input-file]插件使用详解
- Gym - 101246D 博弈
- 第二十二章 李逵为什么不可爱
- [转]JSP中EL表达式三元运算符的使用
- Python 的 sys 模块常用方法
- linux-磁盘分区、挂载
- 射电天文谱线接收机和终端系统
- 暑假周进度总结报告1
- wcf服务契约代理链
- Vuex之state和getters
- delphi7+firebird+dbexpress
- 解决 Chrome最新版右键工具中的编码修改功能没有了的工具
- Python简单模拟微信发红包
- 空间几何变换 之 齐次坐标
- arcgis html图像标记,图片标记
- 天才小毒妃 第914章 无论如何要带她走
- HTML5七夕情人节表白网页制作【樱花雨+爱心3D相册】HTML+CSS+JavaScript
- weinre调试环境搭建
- Psins代码解析之test_SINS_east_west.mtest_SINS_north_south.m
- 数字孪生坦克装置,让智慧战场监管透明化