{"id":109,"date":"2009-11-05T23:01:17","date_gmt":"2009-11-05T15:01:17","guid":{"rendered":"http:\/\/hgc43160.chinaw3.com\/?p=109"},"modified":"2009-11-05T23:01:17","modified_gmt":"2009-11-05T15:01:17","slug":"%e9%9d%9e%e9%9d%99%e6%80%81%e7%b1%bb%e6%88%90%e5%91%98%e5%87%bd%e6%95%b0%e6%8c%87%e9%92%88%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"http:\/\/www.hyzgame.com\/?p=109","title":{"rendered":"\u975e\u9759\u6001\u7c7b\u6210\u5458\u51fd\u6570\u6307\u9488\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"<p align=\"left\"><span style=\"font-family: Tahoma;\"> \u4f17\u6240\u5468\u77e5\uff0c\u6211\u4eec\u65e0\u6cd5\u5b9a\u4e49\u4e00\u4e2a\u6307\u5411\u7c7b\u7684\u975e\u9759\u6001\u6210\u5458\u51fd\u6570\u3002\u5728<strong>Borland  C\/C++<\/strong>\u4e2d\uff0c<strong>Bolrand<\/strong>\u516c\u53f8\u6dfb\u52a0\u4e86\u5173\u952e\u5b57<strong>__closure<\/strong>\u7528\u6765\u5b9a\u4e49\u8fd9\u79cd\u7279\u6b8a\u7684\u6307\u9488\u3002\u5728<strong>Visual C\/C++<\/strong>\u4e2d\u867d\u7136\u4e5f\u6709\u89e3\u51b3\u65b9\u67d3\uff0c\u4f46\u662f\u4f7f\u7528\u9ebb\u70e6\uff0c\u4e14\u4e0d\u6613\u4e8e\u4f7f\u7528\u3002\u7f51\u7edc\u4e0a\u4e0b\u5b58\u5728\u5f88\u591a\u89e3\u51b3\u65b9\u6848\uff0c\u4f46\u4f5c\u8005\u5747\u672a\u770b\u5230\u8f83\u4e3a\u597d\u7528\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/span><\/p>\n<p align=\"left\"><span style=\"font-family: Tahoma;\"> \u672c\u6587\u5373\u63d0\u4f9b\u4e00\u79cd\u4e8e<strong>x86-32<\/strong>\u5e73\u53f0\u4e0b\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4f7f\u7528\u65b9\u5f0f\u7c7b\u4f3c\u4e8e<strong>Borland  C\/C++<\/strong>\uff0c\u5341\u5206\u65b9\u4fbf\u3002\u4ee3\u7801\u5728<strong>Borland C\/C++ 5.82<\/strong>\u548c<strong>Microsoft C\/C++ 14.0 (Visual  C\/C++ 8.0)<\/strong>\u4e0b\u6d4b\u8bd5\u901a\u8fc7\u3002<\/span><\/p>\n<hr size=\"1\" \/>\n<pre>#include&lt;stdio.h&gt;\r\n#include&lt;stddef.h&gt;\r\n\r\nclass _Object{};\r\n\r\n#if defined(SetEventCall)||defined(CallEvent)||defined(DefineEvent)\r\n    #error SetEventCall,CallEvent,DefineEvent \u5df2\u7ecf\u5b9a\u4e49\r\n#endif\/\/\r\n\r\n#ifdef __BORLANDC__\r\n\r\n    #define SetEventCall(event_obj,event_func)              event_obj=event_func\r\n    #define CallEvent(event_obj)                            event_obj\r\n    #define DefineEvent(name,result,intro)                  result (__closure *name)intro\r\n\r\n#else\r\n\r\n    #pragma warning(disable:4311)\r\n\r\n    template &lt;typename T&gt; struct EventFunc\r\n    {\r\n        unsigned __int32 this_address;\r\n        unsigned __int32 func_address;\r\n\r\n        _Object *This;\r\n        T Func;\r\n\r\n        EventFunc()\r\n        {   \r\n            this_address=offsetof(EventFunc,This);\r\n            this_address+=(unsigned __int32)this;\r\n\r\n            func_address=offsetof(EventFunc,Func);\r\n            func_address+=(unsigned __int32)this;\r\n        }\r\n    };\r\n\r\n    #define SetEventCall(event_obj,event_func)      {   \\\r\n                                                        unsigned __int32 this_address=event_obj.this_address;   \\\r\n                                                        unsigned __int32 func_address=event_obj.func_address;   \\\r\n                                                        \\\r\n                                                        {   \\\r\n                                                            __asm mov eax,this_address  \\\r\n                                                            __asm mov ebx,this  \\\r\n                                                            __asm mov [eax],ebx \\\r\n\\\r\n                                                            __asm mov eax,func_address  \\\r\n                                                            __asm mov ebx,event_func    \\\r\n                                                            __asm mov [eax],ebx \\\r\n                                                        }   \\\r\n                                                    }\r\n\r\n    #define CallEvent(event_obj)                    (event_obj.This-&gt;*(event_obj.Func))             \/\/(*(event_obj.This).*(event_obj.Func))\r\n\r\n    #define DefineEvent(name,result,intro)          EventFunc&lt;result (_Object:: *)intro&gt; name;\r\n#endif\/\/__BORLANDC__\r\n\r\nclass Button\r\n{\r\npublic:\r\n\r\n    DefineEvent(OnClick,void,(Button *,int));     \/\/\u5b9a\u4e49\u4e8b\u4ef6,\u539f\u578b\u4e3a: void OnClick(Button *,int)\r\n\r\npublic:\r\n\r\n    Button()\r\n    {\r\n        printf(\"Button this=%p\\n\",this);\r\n    }\r\n\r\n    void TestButtonClick()\r\n    {\r\n        CallEvent(OnClick)(this,0);               \/\/\u547c\u53ebOnClick,\u539f\u578b\u4e3a: OnClick(this,0)\r\n    }\r\n};\r\n\r\nclass Test\r\n{\r\n    Button *button;\r\n\r\npublic:\r\n\r\n    void OnButtonClick(Button *but,int)\r\n    {\r\n        printf(\"Test::OnButtonClick,this=%p,but=%p\\n\",this,but);\r\n    };\r\n\r\npublic:\r\n\r\n    Test()\r\n    {\r\n        printf(\"Test this=%p\\n\",this);\r\n\r\n        button=new Button;\r\n\r\n        SetEventCall(button-&gt;OnClick,OnButtonClick);              \/\/\u8bbe\u5b9abutton-&gt;OnClick\u4e8b\u4ef6\u7684\u5904\u7406\u51fd\u6570\u4e3aOnButtonClick\r\n\r\n        button-&gt;TestButtonClick();\r\n    }\r\n};\r\n\r\nvoid main(int,char **)\r\n{\r\n    Test *test;\r\n\r\n#ifdef __BORLANDC__\r\n    printf(\"Compiler: Borland C\/C++ or Turbo C\/C++ %d.%d%d\\n\",(__BORLANDC__&gt;&gt;8),((__BORLANDC__&amp;0xF0)&gt;&gt;4),(__BORLANDC__&amp;0x0F));\r\n#endif\r\n#ifdef _MSC_VER\r\n    printf(\"Compiler: Microsoft C\/C++ %.2f (Visual C\/C++ %.2f)\\n\",_MSC_VER\/100.f,_MSC_VER\/100.f-6);\r\n#endif\/\/\r\n\r\n    printf(\"Compile Time: %s %s\\n\\n\",__DATE__,__TIME__);\r\n\r\n    test=new Test;\r\n\r\n    delete test;\r\n}<\/pre>\n<hr size=\"1\" \/>\n<pre>Compiler: Borland C\/C++ or Turbo C\/C++ 5.82\r\nCompile Time: Dec 23 2006 17:34:48\r\n\r\nTest this=00902D50\r\nButton this=00902D64\r\nTest::OnButtonClick,this=00902D50,but=00902D64<\/pre>\n<hr size=\"1\" \/>\n<pre>Compiler: Microsoft C\/C++ 14.00 (Visual C\/C++ 8.00)\r\nCompile Time: Dec 23 2006 17:34:00\r\n\r\nTest this=003826D0\r\nButton this=00382700\r\nTest::OnButtonClick,this=003826D0,but=00382700<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f17\u6240\u5468\u77e5\uff0c\u6211\u4eec\u65e0\u6cd5\u5b9a\u4e49\u4e00\u4e2a\u6307\u5411\u7c7b\u7684\u975e\u9759\u6001\u6210\u5458\u51fd\u6570\u3002\u5728Borland C\/C++\u4e2d\uff0cBolrand\u516c\u53f8\u6dfb\u52a0\u4e86\u5173\u952e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-cc"],"_links":{"self":[{"href":"http:\/\/www.hyzgame.com\/index.php?rest_route=\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.hyzgame.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.hyzgame.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.hyzgame.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.hyzgame.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=109"}],"version-history":[{"count":1,"href":"http:\/\/www.hyzgame.com\/index.php?rest_route=\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":110,"href":"http:\/\/www.hyzgame.com\/index.php?rest_route=\/wp\/v2\/posts\/109\/revisions\/110"}],"wp:attachment":[{"href":"http:\/\/www.hyzgame.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.hyzgame.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.hyzgame.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}