首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

thinking in cpp 中 的一个小疑点

2012-03-12 
thinking in cpp 中 的一个小问题#includeiostreamusingnamespacestdclassDog{public:intrun(inti)cons

thinking in cpp 中 的一个小问题
#include   <iostream>
using   namespace   std;

class   Dog   {
public:
    int   run(int   i)   const   {  
        cout   < <   "run\n ";    
        return   i;  
    }
    int   eat(int   i)   const   {  
          cout   < <   "eat\n ";    
          return   i;  
    }
    int   sleep(int   i)   const   {  
        cout   < <   "ZZZ\n ";  
        return   i;  
    }
    typedef   int   (Dog::*PMF)(int)   const;
    //   operator-> *   must   return   an   object  
    //   that   has   an   operator():
    class   FunctionObject   {
        Dog*   ptr;
        PMF   pmem;
    public:
        //   Save   the   object   pointer   and   member   pointer
        FunctionObject(Dog*   wp,   PMF   pmf)  
            :   ptr(wp),   pmem(pmf)   {  
            cout   < <   "FunctionObject   constructor\n ";
        }
        //   Make   the   call   using   the   object   pointer
        //   and   member   pointer
        int   operator()(int   i)   const   {
            cout   < <   "FunctionObject::operator()\n ";
            return   (ptr-> *pmem)(i);   //   Make   the   call
        }
    };
    FunctionObject   operator-> *(PMF   pmf)   {  
        cout   < <   "operator-> * "   < <   endl;
        return   FunctionObject(this,   pmf);
    }
};
 
int   main()   {
    Dog   w;
    Dog::PMF   pmf   =   &Dog::run;
    cout   < <   (w-> *pmf)(1)   < <   endl;
    pmf   =   &Dog::sleep;
    cout   < <   (w-> *pmf)(2)   < <   endl;
    pmf   =   &Dog::eat;
    cout   < <   (w-> *pmf)(3)   < <   endl;
}   ///:~


typedef   int   (Dog::*PMF)(int)   const;       这句代码是什么意思    
还有int   operator()(int   i)   const   {
            cout   < <   "FunctionObject::operator()\n ";
            return   (ptr-> *pmem)(i);   //   Make   the   call
        }


FunctionObject   operator-> *(PMF   pmf)   {  


        cout   < <   "operator-> * "   < <   endl;
        return   FunctionObject(this,   pmf);
    }
  这两段代码不懂

[解决办法]
C++ Primer上找“指向成员的指针”

热点排行