• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            flyman

            opengl world
            隨筆 - 10, 文章 - 0, 評(píng)論 - 65, 引用 - 0
            數(shù)據(jù)加載中……

            C++ 類成員的CALLBACK

            這幾天在用CEGUI,學(xué)習(xí)了一下他的CALLBACK原理,寫了一個(gè)小CASE
            FOLLOWING IS IT:
              1 // main.cpp : Defines the entry point for the console application.
              2 //
              3 
              4 #include "stdafx.h"
              5 #include <string>
              6 #include <iostream>
              7 #include <map>
              8 
              9 class MessageMap//功能類,調(diào)用對(duì)象
             10 {
             11 public:
             12     MessageMap()
             13     {
             14 
             15     }
             16     ~MessageMap()
             17     {
             18 
             19     }
             20     int print(int para1,int para2)
             21     {
             22         std::cout<<"Para1="<<para1<<std::endl;
             23         std::cout<<"para2="<<para2<<std::endl;
             24         return 1;
             25     }
             26 
             27     int add(int para1,int para2)
             28     {
             29         std::cout<<"para1+para2="<<para1+para2<<std::endl;
             30         return 1;
             31     }
             32 };
             33 
             34 typedef int (MessageMap::*MemberFunction)(int ,int );//Callback函數(shù)原型
             35 
             36 class FuncCode//函數(shù)的從屬關(guān)系
             37 {
             38 
             39 public:
             40     FuncCode(MessageMap* pObj,MemberFunction pFun)
             41     {
             42         obj=pObj;
             43         fun=pFun;
             44     }
             45 public:
             46 
             47     MessageMap* obj;
             48     MemberFunction fun;
             49 };
             50 
             51 class SendMessage//調(diào)用類
             52 {
             53 public:
             54     SendMessage()
             55     {
             56 
             57     }
             58     ~SendMessage()
             59     {
             60         FunMapIterator itend=funmap.end();
             61         for (FunMapIterator it=funmap.begin ();it!=itend;it++)
             62         {
             63             delete it->second;
             64         }
             65         funmap.clear ();
             66     }
             67     int addMessageFunction(std::string msg,int (MessageMap::*fun)(int,int),MessageMap* pobj)
             68     {
             69         funmap[msg]=new FuncCode(pobj,fun);
             70         return 1;
             71     }
             72      int operator()(std::string msg,int para1,int para2)
             73     {
             74          return ((funmap[msg]->obj)->*(funmap[msg]->fun))(para1,para2);
             75         
             76     }
             77 protected:
             78     typedef std::map<std::string,FuncCode*> FunMap;
             79     typedef std::map<std::string,FuncCode*>::iterator FunMapIterator;
             80     FunMap funmap;
             81 };
             82 
             83 int _tmain(int argc, _TCHAR* argv[])
             84 {
             85     MessageMap* pObj= new MessageMap();
             86     SendMessage SendMsg;
             87     {//初始化
             88         SendMsg.addMessageFunction ("print",&MessageMap::print,pObj);
             89         SendMsg.addMessageFunction ("add",&MessageMap::add,pObj);
             90     }
             91 
             92     {//調(diào)用
             93         SendMsg("print",1,2);
             94         SendMsg("add",1,2);
             95     }
             96 
             97     delete pObj;
             98     return 0;
             99 }
            100 
            101 //說明
            102 //1、這種調(diào)用可以用類模板擴(kuò)展,其實(shí)這是一個(gè)CALLBACK簡(jiǎn)略版,有興趣的話可以參考CEGUI源碼,里面用的是類模板
            103 //,這樣的話將不再受類型(MESSAGEMAP)的限制。
            104 //
            105 //2、對(duì)于int addMessageFunction(std::string msg,int (MessageMap::*fun)(int,int),MessageMap* pobj)
            106 //的參數(shù)問題,主要說明int (MessageMap::*fun)(int,int)。
            107 //這是一個(gè)很有意思的參數(shù),他的類型為int (MessageMap::*)(int,int),值為 fun,有興趣的可以看看ASM的傳參過程
            108 //,其實(shí)這里可以用MemberFunction fun代替,當(dāng)然用模板的話會(huì)有所不同,參考CEGUI.
            109 //3.不要把typedef int (MessageMap::*MemberFunction)(int ,int ) 定義成
            110 //typedef int (*MemberFunction)(int ,int ),這是代碼的關(guān)鍵所在,一般的C++BOOK都會(huì)提及他們的不同之處。
            111 
            112 
            113 
            114 


            posted on 2007-08-10 20:57 flyman 閱讀(3726) 評(píng)論(5)  編輯 收藏 引用

            評(píng)論

            # re: C++ 類成員的CALLBACK  回復(fù)  更多評(píng)論   

            總結(jié)的不錯(cuò), 比較巧妙的例子
            2007-08-10 23:27 | SmartPtr

            # re: C++ 類成員的CALLBACK  回復(fù)  更多評(píng)論   

            不錯(cuò),寫的非常的好!
            2007-08-13 09:34 | 夢(mèng)在天涯

            # re: C++ 類成員的CALLBACK  回復(fù)  更多評(píng)論   

            其實(shí)主要是使用了.*或者是-〉*操作符的表達(dá)式。
            2007-08-21 09:51 | SuperPlayeR

            # re: C++ 類成員的CALLBACK  回復(fù)  更多評(píng)論   

            不錯(cuò)的例子
            2007-09-23 09:32 | 螞蟻終結(jié)者

            # re: C++ 類成員的CALLBACK  回復(fù)  更多評(píng)論   


            template < class Class, typename ReturnType, typename Parameter1, typename Parameter2 >
            class CallBack < Class, ReturnType, Parameter1, Parameter2, void >
            {

            public:

            typedef ReturnType (Class::*Method)(Parameter1,Parameter2);

            CallBack(Class* _class_instance, Method _method)
            {
            class_instance = _class_instance;
            method = _method;
            };

            ReturnType operator()(Parameter1 parameter1, Parameter2 parameter2)
            {
            return (class_instance->*method)(parameter1, parameter2);
            };

            ReturnType execute(Parameter1 parameter1, Parameter2 parameter2)
            {
            return operator()(parameter1, parameter2);
            };

            private:

            Class* class_instance;
            Method method;

            };

            typedef CallBack<MessageMap, int, int, int> callback;

            MessageMap messageMap;
            vector<callback> callbackList;
            callbackList.push_back(callback(&messageMap,&MessageMap::add));
            callbackList.push_back(callback(&messageMap,&MessageMap::print));

            for (unsigned int i =0; i<callbackList.size(); i++)
            {
            callbackList[i].execute(1, 2);

            }


            // more simpler?
            2009-01-07 06:29 | vistors

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            精品国际久久久久999波多野| 亚洲成色999久久网站| 青青草原综合久久大伊人| 伊人久久大香线蕉综合热线| 亚洲色婷婷综合久久| 中文字幕久久欲求不满| 一级a性色生活片久久无| 99精品国产综合久久久久五月天| 97精品久久天干天天天按摩| 99久久精品九九亚洲精品| 久久人人爽人人爽人人片AV不 | 久久99亚洲网美利坚合众国| 国产精品gz久久久| 蜜臀av性久久久久蜜臀aⅴ| 狠狠色综合久久久久尤物| 少妇高潮惨叫久久久久久| 精品久久久久久国产牛牛app| 久久婷婷五月综合97色| 一本久久免费视频| 精品综合久久久久久88小说| 国产成人综合久久综合| 亚洲∧v久久久无码精品| 伊人情人综合成人久久网小说| 亚洲午夜久久久精品影院| 亚洲色大成网站www久久九| 久久国产午夜精品一区二区三区| 国产成人久久精品激情 | 国产精品99精品久久免费| 中文字幕久久亚洲一区| 久久精品国产亚洲5555| 99久久国产亚洲高清观看2024| 国内精品久久久久影院日本| 色综合久久综合中文综合网| 中文字幕人妻色偷偷久久| 久久国产精品无| 亚洲日本久久久午夜精品| 波多野结衣久久一区二区| 国内精品九九久久精品| 色欲久久久天天天综合网| 亚洲国产精品久久电影欧美| 亚洲色大成网站www久久九|