博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【C++】指向函数的指针,指向重载函数的指针,指向类成员的指针
阅读量:6989 次
发布时间:2019-06-27

本文共 1188 字,大约阅读时间需要 3 分钟。

hot3.png

  1. 指向函数的指针
    函数的类型由它的返回值和参数列表决定, 但函数不能返回一个函数类型。
    int fce( const char*, ... );int fc( const char* );// point to fcetypedef int (*PFCE)( const char*, ... );// point to fctypedef int (*PFC)( const char* );// InitializationPFCE pfce = fce;PFC pfc = fc;// Invoke the functionspfce();pfc();
  2. 指向重载函数的指针
    在两个函数指针类型之间不能进行类型转换, 必须严格匹配才能找到相应的函数。
    void ff( const char* );void ff( unsigned int );// Error: No Match, not available parameter listvoid (*pf)(int) = ff;// Error: No Match, not available return typedouble (*pf2)( const char* )) = ff;
  3. 指向类实例成员函数的指针
    指向类实例成员函数的指针必须匹配三个方面:参数类型和个数,返回类型,所属类类型。
    class Screen{public:	int Add(int lhs, int rhs);	int Del( int lhs, int rhs );	void ShowNumber(int value);};typedef int (Screen::*OPE)( int, int);typedef void (Screen::*SHOW)( int);OPE ope = &Screen::Add;SHOW show = &Screen::ShowNumber;Screen scr;	// Invoke its own memberint value = scr.Add(10, 20);scr.ShowNumber(value);// Invoke the function pointvalue = (scr.*ope)(10, 20);(scr.*show)(value);
  4. 指向类静态成员函数的指针
    指向类静态成员函数的指针属于普通指针。
    class Screen{public:	static int Add(int lhs, int rhs);};typedef int (*OPE)(int, int);OPE ope = &Screen::Add;int value = ope(10, 20);

转载于:https://my.oschina.net/freedomart/blog/125788

你可能感兴趣的文章