xyjzsh

指向类成员对象的指针

定义:
class Shape
{
public:
//...
void moveTo(Point newLocation);
bool validate()const;
vritual bool draw() const=0;
//...
}:
class Circle: public Shape
{
//...
bool draw() const;
//...
};

void (Shape::*mf1)(Point ) = &Shape::moveTo;//指向类的成员函数的指针。

ReturnType (ClassName::*pFuncName)(paramList);
pFuncName定义了指向ClassName中的一组函数。这组函数的形式是返回值为ReturnType,函数列表为paramList.

ClassName的限定使得只有ClassName或派生自它的类的对象才能解引用这个函数指针。

  1 #include"stdafx.h"
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 
  5 class Base;
  6 class Derived;
  7 typedef long (Base::*PFunc)(long,long);
  8 
  9 enum FuncType
 10 {
 11     ADD=1,
 12     SUB,
 13     DIV,
 14     MUL,
 15 };
 16 
 17 typedef struct tagCallBackFunc
 18 {
 19     long funcType;
 20     PFunc proc;
 21 }COMMAND,*PCOMMAND;
 22 
 23 class Base
 24 {
 25 public:
 26 
 27     virtual PCOMMAND GetCommands(void);
 28     static COMMAND command[];
 29 
 30     long base_add(long a,long b){printf("in base_add()\n");return a+b;}
 31     long base_sub(long a,long b){printf("in base_sub()\n");return a-b;}
 32     long base_div(long a,long b){printf("in base_div()\n");return a/b;}
 33     long base_mul(long a,long b){printf("in base_mul()\n");return a*b;}
 34 };
 35 
 36 COMMAND Base::command[]={
 37                        {ADD,(PFunc)&Base::base_add},
 38                        {SUB,(PFunc)&Base::base_sub},
 39                        {MUL,(PFunc)&Base::base_mul},
 40                        {DIV,(PFunc)&Base::base_div},
 41                   };
 42 PCOMMAND Base::GetCommands()
 43 {
 44     return command;
 45 }
 46 
 47 class Derived:public Base
 48 {
 49 public:
 50     Derived(void){}
 51     ~Derived(void){}
 52     virtual PCOMMAND GetCommands(void){return command;}
 53 
 54     long add(long a,long b){printf("in add()\n");return a+b;}
 55     long sub(long a,long b){printf("in sub()\n");return a-b;}
 56     long div(long a,long b){printf("in div()\n");return a/b;}
 57     long mul(long a,long b){printf("in mul()\n");return a*b;}
 58 
 59     static COMMAND command[];
 60 };
 61 
 62 COMMAND Derived::command[]=
 63                    {
 64                        {ADD,(PFunc)&Derived::add},
 65                        {SUB,(PFunc)&Derived::sub},
 66                        {MUL,(PFunc)&Derived::mul},
 67                        {DIV,(PFunc)&Derived::div},
 68                        {0}
 69                     };    
 70 
 71 
 72 
 73 void  test(Base *control,FuncType funcType,long operand1,long operand2)
 74 {
 75     PCOMMAND pCommand = control->GetCommands();
 76     PCOMMAND pNowCommand=NULL;
 77 
 78     for(long i=0;pCommand[i].funcType;++i)
 79     {
 80         if(funcType == pCommand[i].funcType)
 81         {
 82             pNowCommand = &pCommand[i]; 
 83             break;
 84         }
 85     }
 86 
 87     if(pNowCommand)
 88     {
 89         long res = (control->*pNowCommand->proc)(operand1,operand2);
 90         printf("res=%d\n",res);
 91     }
 92 }
 93 
 94 int main()
 95 {
 96     Derived *=new Derived();
 97     Base *= (Base*)d;
 98     test(b,ADD,1,2);
 99 
100     Base *bb = new Base;
101     test(bb,MUL,1,2);
102     delete bb;
103     delete d;
104     return 0;
105 }

第89行是对指向类成员函数的指针的解析。

posted on 2010-12-15 14:00 呆人 阅读(279) 评论(0)  编辑 收藏 引用 所属分类: 数据结构


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2010年12月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜