函数指针,成员函数指针,重载函数指针
2014-05-30 16:04
302 查看
指向函数的指针
函数的类型由它的返回值和参数列表决定, 但函数不能返回一个函数类型。
指向重载函数的指针
在两个函数指针类型之间不能进行类型转换, 必须严格匹配才能找到相应的函数。
指向类实例成员函数的指针
指向类实例成员函数的指针必须匹配三个方面:参数类型和个数,返回类型,所属类类型。
指向类静态成员函数的指针
指向类静态成员函数的指针属于普通指针。
view
source
print?
函数的类型由它的返回值和参数列表决定, 但函数不能返回一个函数类型。
01 | int fce( const char *, ... ); |
02 | int fc( const char * ); |
03 |
04 | // point to fce |
05 | typedef int (*PFCE)( const char *, ... ); |
06 | // point to fc |
07 | typedef int (*PFC)( const char * ); |
08 |
09 | // Initialization |
10 | PFCE pfce = fce; |
11 | PFC pfc = fc; |
12 |
13 | // Invoke the functions |
14 | pfce(); |
15 | pfc(); |
在两个函数指针类型之间不能进行类型转换, 必须严格匹配才能找到相应的函数。
1 | void ff( const char * ); |
2 | void ff( unsigned int ); |
3 |
4 | // Error: No Match,not available parameter list |
5 | void (*pf)( int ) = ff; |
6 | // Error: No Match,not available return type |
7 | double (*pf2)( const char * )) = ff; |
指向类实例成员函数的指针必须匹配三个方面:参数类型和个数,返回类型,所属类类型。
01 | class Screen |
02 | { |
03 | public : |
04 | int Add( int lhs, int rhs); |
05 | int Del( int lhs, int rhs ); |
06 | void ShowNumber( int value); |
07 | }; |
08 |
09 | typedef int (Screen::*OPE)( int , int ); |
10 | typedef void (Screen::*SHOW)( int ); |
11 |
12 | OPE ope = &Screen::Add; |
13 | SHOW show = &Screen::ShowNumber; |
14 |
15 | Screen scr; |
16 |
17 | // Invoke its own member |
18 | int value |
19 | scr.ShowNumber(value); |
20 |
21 | // Invoke the function point |
22 | value |
23 | (scr.*show)(value); |
指向类静态成员函数的指针属于普通指针。
view
source
print?
01 | class Screen |
02 | { |
03 | public : |
04 | static int Add( int lhs, int rhs); |
05 | }; |
06 |
07 | typedef int (*OPE)( int , int ); |
08 |
09 | OPE ope = &Screen::Add; |
10 |
11 | int value |
相关文章推荐
- 函数指针之成员函数指针
- 函数指针和类成员函数指针的测试例子
- 一般函数指针的用法与类成员的函数指针
- 指向成员函数指针,虚函数指针,静态成员函数指针
- 函数指针与成员函数指针-C++委托
- c++中的函数指针和类成员函数指针
- 函数指针、this指针、bind函数、成员函数指针
- 类成员函数的函数指针与返回值为函数指针的函数
- 函数指针以及成员函数指针1
- 函数指针 类成员函数指针 类静态成员函数指针 使用实例
- 指向成员函数的指针类型成员变量
- 成员函数指针与高性能的C++委托(下篇)
- C++的类成员函数指针
- C++ Primer 指向成员函数的指针
- C++成员函数指针的应用
- 线程的封装技巧:把任意类的成员函数指针作为参数
- 成员函数指针与高性能的C++委托
- 获得全局函数和成员函数指针的部分traits的方法
- 怎样将成员函数指针强制转换成void*指针?
- 成员函数指针【转贴】