游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1422|回复: 0

转载 : 我的Cpp (一) - Cpp 类成员函数指针分类

[复制链接]

4

主题

5

帖子

5

积分

新手上路

Rank: 1

积分
5
QQ
发表于 2008-7-21 23:21:00 | 显示全部楼层 |阅读模式
  toophy 原创, email : toophy@126.com, 工作几年积累的一点经验, 拿来夯实一下

例子:


class CPlant

{

  int age;

  int height;

  int weight;


  int birth( int nheight, int nweight )

  {

     age = 0;

     height = nheight;

     weight = nweight;

     return 1;

  }

};


class CPotato : public CPlant

{

  int wet;

  

  int birth( int nheight, int nweight )

  {

   CPlant::birth(nheight,nweight);

   wet = 0;

   return 1;

  }

  

  int changewet( int nwet )

  {

    int old = wet;

    wet = nwet;

    return old;

  }

};


class CAnimal

{

int age;

int height;

int weight;

int domain; // 地盘


int birth( int nheight, int nweight )

{

   height = nheight;

   weight = nweight;

   age = 0;

   domain = 0;

   return 1;

}


int changedomain( int ndomain )

{

   int old = domain;

   domain = ndomain;

   return old;

}

};


// 指针类型, 用于 birth

// 为了统一管理 birth 现象


typedef int(CPlant::*BIRTHPROC)(int,int);


// 声明, 可以保存为一个数组, 元素是 BIRTHPROC 指针

BIRTHPROC  plantBirth;

BIRTHPROC  potatoBirth;

BIRTHPROC  animalBirth;


// 定义

plantBirth = &CPlant::birth;

potatoBirth = (BIRTHPROC)&CPotato::birth;

animalBirth = (BIRTHPROC)&CAnimal::birth;


CPlant plant;

CPotato potato;

CAnimal animal;


// 调用 **

(plant.*plantBirth)(10,2);

(potato.*potatoBirth)(6,4);

( ( (CPlant*)&animal )->*animalBirth )(100,30);


通过这个例子, 可以看出, 定义指针变量时只要使用 “强制转换” , 就可以通过编译, 注意赋值时候格式 “ & xxx :: FuncName “ , 就是取出函数跳转地址, 这样看来, 指针变量保存的

数据不过就是 函数跳转地址, 使用void* 也可以做到, 但是 后者在调用时候就很麻烦, 要写成 …..


调用时, 继承类型的好处是 : 和父类一样的调用方式. 没有任何关系的类型, 就要写成animal 的调用方式, 用 CPlant* 来强制转换来通过编译, 不要认为这样调用比上面两个慢,

其实, 汇编结果都一样, 如下


(plant.*plantBirth)(10,2);

push        2

push        10   

lea         ecx,[plant]

call        dword ptr [plantBirth]


(potato.*potatoBirth)(6,4);

push        4

push        6   

lea         ecx,[potato]

call        dword ptr [potatoBirth]


( ( (CPlant*)&animal )->*animalBirth )(100,30);

push        30   

push        100

lea         ecx,[animal]

call        dword ptr [animalBirth]


2008.7.21 22:28 作者:toophy 引用:0 | 收藏 | 评论:0
[url=http://toophy.bokee.com]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-21 22:33

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表