{"id":122,"date":"2010-07-29T23:14:51","date_gmt":"2010-07-29T15:14:51","guid":{"rendered":"http:\/\/blog.mega-zone.org\/?p=122"},"modified":"2010-08-12T00:09:57","modified_gmt":"2010-08-11T16:09:57","slug":"%e9%82%aa%e6%81%b6%e7%9a%84member-function-pointer","status":"publish","type":"post","link":"https:\/\/blog.mega-zone.org\/?p=122","title":{"rendered":"\u90aa\u6076\u7684member-function pointer"},"content":{"rendered":"<blockquote><p><strong>\u8c01\u90fd\u7528\u8fc7\u6307\u9488\u3002<\/strong><\/p>\n<p><strong>\u66f4\u522b\u8bf4\u6211\u548c\u4f60\u4e86\u3002<\/strong><\/p>\n<p><strong>\u4f46\u662f\u5f53member-function pointer\u51fa\u73b0\u7684\u65f6\u5019\uff0c<\/strong><\/p>\n<p><strong>\u4f60\u8fd8\u662f\u8981\u8dea\u5012\uff01<\/strong><\/p><\/blockquote>\n<p>\u55ef\u2026\u2026\u4eca\u5929\u8017\u4e86\u4e9b\u7cbe\u529bd\u6389\u4e00\u4e2a\u8001bug\u3002\u75c7\u72b6\u662f\u8fd9\u6837\u7684\uff0c\u4ece\u67d0\u4eba\u5904\u62f7\u8d1d\u6765\u7684static lib\u94fe\u63a5\u5230\u5e94\u7528\u7a0b\u5e8f\u4e4b\u540e\uff0clib\u91cc\u9762\u7684\u51fd\u6570\u5c1d\u8bd5\u6267\u884c\u5e94\u7528\u7a0b\u5e8f\u4f20\u8fc7\u53bb\u7684callback\uff0c\u7ed3\u679c\u6ca1\u53cd\u5e94\u3002<\/p>\n<p>\u4f20\u5165\u7684callback\u662f\u4e00\u4e2a\u5305\u542b\u4e86\u67d0member-function pointer(MFP)\u7684\u7ed3\u6784\u4f53\uff0c\u5927\u81f4\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">typedef void (Object::*FunctionPtr)(void);\r\n\r\nstruct Callback {\r\nchar * szName;\r\nFunctionPtr fp;\r\n}<\/pre>\n<p>\u5e93\u51fd\u6570\u5927\u81f4\u4e3a\u8fd9\u4e2a\u6837\u5b50\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">void foo(Callback* ptr, Object* pObj) {\r\n\r\nif(pObj &amp;&amp; ptr-&gt;fp)\r\n(pObj-&gt;*(ptr-&gt;fp))(); \/\/\u600e\u4e48\u8fd9\u4e48\u96be\u770b\uff0c\u6211\u5199\u5bf9\u4e86\u4f10\u2026\u2026\r\n\r\n}<\/pre>\n<p>\u5f00\u59cb\u4ee5\u4e3a\u662f\u4ec0\u4e48\u53c2\u6570\u9519\u8bef\uff0c\u540e\u6765\u60ca\u5947\u7684\u53d1\u73b0\u5728\u672c\u5730\u91cd\u65b0\u7f16\u8bd1\u4e00\u6b21static lib\uff0c\u518d\u94fe\u63a5\u5c31\u4e00\u5207\u6b63\u5e38\u50da\u3002\u65e2\u7136\u8fd9\u6837\u80af\u5b9a\u662f\u7f16\u8bd1\u51fa\u6765\u7684asm\u6709\u95ee\u9898\uff0c\u7565\u53bb\u75db\u82e6\u7684\u8fc7\u7a0b\uff0c\u76f4\u63a5\u8fdb\u5165\u4e3b\u9898\uff0c\u4e00\u8def\u8ddf\u8fc7\u53bb\u53d1\u73b0\u4e86\u4e0d\u540c\u3002\u5728\u9519\u8bef\u7684lib\u4e2d\uff0cptr-&gt;fp\u7684\u5730\u5740\u662fptr+0x8h\uff0c\u800c\u6b63\u786e\u7684lib\u91cc\u9762\u662fptr+0x10h\u3002\u4e8e\u662f\u53bb\u770b\u4e86\u4e00\u4e0b\u6211\u672c\u5730\u7f16\u8bd1\u51fa\u6765\u7684sizeof(Callback)\uff0c\u731c\u731c\u662f\u591a\u5c11\uff1f<\/p>\n<p>\u7b54\u6848\u662f32\u3002<\/p>\n<p>\u4e3a\u5565\u8fd9\u4e48\u5927\u554a\uff0c\u4e8e\u662f\u53c8\u6c42\u4e86\u4e0bsizeof(fp)\u3002\u731c\u731c\u770b\u8fd9\u6b21\u53c8\u662f\u591a\u5c11\uff1f<\/p>\n<p>\u7b54\u6848\u662f16\u3002<\/p>\n<p><del datetime=\"2010-08-03T16:12:14+00:00\">\u672c\u6765\u4ee5\u4e3afp\u81f3\u591a\u518d\u5b58\u4e2a\u865a\u8868\uff0c\u66f4\u4f55\u51b5\u6211\u8fd9\u4e2a\u662fnon-virtual\u7684\uff0c\u4e0d\u8981\u865a\u8868\uff0c\u7ed3\u679c\u6ca1\u60f3\u5230\u641e\u51fa\u8fd9\u4e48\u5927\u4e00\u5768\u3002\u8fd9\u6837\u770b\u6765\uff0c\u56e0\u4e3a\u8981\u5bf9\u9f50\u6240\u4ee5\u6574\u4e2aCallback\u7ed3\u6784\u4f53\u88ab\u641e\u6210\u4e8632\u5b57\u8282\uff0c\u8fd9\u4e2achar*\u53ef\u771f\u662f\u6d6a\u8d39\u5462\u3002\u7531\u6b64\u63a8\u51fa\u9519\u8bef\u7684lib\u91cc\u9762\u662f\u628afp\u5f53\u505a8byte\u5927\u5c0f\u6765\u50a8\u5b58\u7684\uff0c\u6240\u4ee5ptr-&gt;fp\u5c31\u662fptr+0x8h\u3002<\/del>\uff08\u597d\u5427\uff0c\u8fd9\u4e00\u6bb5\u7684\u5206\u6790\u5728\u4e8b\u540e\u8bc1\u660e\u662f\u9519\u7684\uff0cfp\u603b\u662f16byte\uff0c\u51fa\u9519\u662f\u56e0\u4e3a\u9519\u8bef\u7684\u5bf9\u9f50\u89c4\u5219\uff0c\u524d\u8005\u9519\u8bef\u7684\u4f7f\u7528\u4e86#pragma pack(16)\u7684\u89c4\u5219\uff0c\u90aa\u6076\u7684vc6.0 sp5\u2026\u2026\uff09<\/p>\n<p>\u6bd4\u8f83\u5de7\u5408\u7684\u662f\uff0c\u7531\u4e8e\u4f20\u5165\u7684Callback*\u6765\u81ea\u4e00\u4e2a\u50a8\u5b58\u5728mem pool\u91cc\u7684\u6570\u636e\uff0c\u800c\u8fd9\u4e2amem pool\u662f\u7531\u81ea\u5df1\u7684\u5185\u5b58\u5206\u914d\u5668\u6765\u7ba1\u7406\u7684\uff0c\u4e00\u5f00\u59cb\u4f1a\u628apool\u91cc\u7684\u6570\u636e\u5168\u90e8\u51990\uff0c\u6240\u4ee5\u9519\u8bef\u7684\u6307\u9488\u6070\u597d\u6307\u5411\u4e86\u4e00\u6bb5\u4e3a0\u7684\u5185\u5b58\uff0c\u9634\u5dee\u9633\u9519\u7684\u901a\u8fc7\u4e86if\u5224\u65ad\uff0c\u6240\u4ee5\u6ca1\u6709\u62a5access violation\u3002<\/p>\n<p>\u6700\u91cd\u8981\u7684\u662f\u4e3a\u5565\u4f1a\u6709\u8fd9\u4e48\u5927\u7684fp\uff1f\u653e\u72d7\u67e5\u5230\u4e00\u7bc7\u6587\u7ae0\uff0c<a href=\"http:\/\/www.codeproject.com\/KB\/cpp\/FastDelegate.aspx\">\u300aMember Function Pointers and the Fastest Possible C++ Delegates\u300b<\/a>\uff0c\u539f\u6765\u8fd9\u4e48\u590d\u6742\u3002\u5176\u5b9e\u6211\u4ee5\u524d\u8fd8\u7528\u8fc7\u8fd9\u4e2afastdelegate\uff0c\u4f46\u662f\u5b8c\u5168\u6ca1\u6ce8\u610f\u5230\u6587\u7ae0\u4e2d\u6709\u4e2a\u63cf\u8ff0\u4e0d\u540c\u7f16\u8bd1\u5668\u4e0b\u7684member-function pointer\u5927\u5c0f\u7684\u8868\u683c\u3002\u771f\u662f\u70ed\u95f9\u554a\uff0c\u4ece4\u523020\u65e0\u5947\u4e0d\u6709\u2026\u2026\u56de\u5934\u770b\u770b\u6211\u7684\u73af\u5883\uff0c\u5e94\u8be5\u662f\u7531\u4e8eMSVC\u7684\u7f16\u8bd1\u9009\u9879\u4e0d\u540c\u5bfc\u81f4\u4e86\u5927\u5c0f\u4e0d\u540c\u3002\u5177\u4f53\u662f\u88ab\u5f53\u505a\u90a3\u79cd\u60c5\u51b5\u5904\u7406\u4e86\u8fd8\u5f97\u518d\u4ed4\u7ec6\u7814\u7a76\u4e0b\u2026\u2026<\/p>\n<p>\u88f8\u7528member-function pointer\u771f\u662f\u7f6a\u5b7d\u554a\uff0c\u8fd8\u662f\u5c01\u4e00\u5c42\u6bd4\u8f83\u597d\u3002\u6bd4\u5982boost::bind\u90a3\u79cd\u4e1c\u897f\u2026\u2026<\/p>\n<p>\u55ef\uff0c\u603b\u4e4b\u662f\u723d\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8c01\u90fd\u7528\u8fc7\u6307\u9488\u3002 \u66f4\u522b\u8bf4\u6211\u548c\u4f60\u4e86\u3002 \u4f46\u662f\u5f53member-function pointer\u51fa\u73b0\u7684\u65f6\u5019\uff0c \u4f60\u8fd8\u662f\u8981\u8dea\u5012\uff01 \u55ef\u2026\u2026\u4eca\u5929\u8017\u4e86\u4e9b\u7cbe\u529bd\u6389\u4e00\u4e2a\u8001bug\u3002\u75c7\u72b6\u662f\u8fd9\u6837\u7684\uff0c\u4ece\u67d0\u4eba\u5904\u62f7\u8d1d\u6765\u7684static lib\u94fe\u63a5\u5230\u5e94\u7528\u7a0b\u5e8f\u4e4b\u540e\uff0clib\u91cc\u9762\u7684\u51fd\u6570\u5c1d\u8bd5\u6267\u884c\u5e94\u7528\u7a0b\u5e8f\u4f20\u8fc7\u53bb\u7684callback\uff0c\u7ed3\u679c\u6ca1\u53cd\u5e94\u3002 \u4f20\u5165\u7684callback\u662f\u4e00\u4e2a\u5305\u542b\u4e86\u67d0member-function pointer(MFP)\u7684\u7ed3\u6784\u4f53\uff0c\u5927\u81f4\u5b9a\u4e49\u5982\u4e0b\uff1a typedef void (Object::*FunctionPtr)(void); struct Callback { char * szName; FunctionPtr fp; } \u5e93\u51fd\u6570\u5927\u81f4\u4e3a\u8fd9\u4e2a\u6837\u5b50\uff1a void foo(Callback* ptr, Object* pObj) { if(pObj &amp;&amp; ptr-&gt;fp) (pObj-&gt;*(ptr-&gt;fp))(); \/\/\u600e\u4e48\u8fd9\u4e48\u96be\u770b\uff0c\u6211\u5199\u5bf9\u4e86\u4f10\u2026\u2026 } \u5f00\u59cb\u4ee5\u4e3a\u662f\u4ec0\u4e48\u53c2\u6570\u9519\u8bef\uff0c\u540e\u6765\u60ca\u5947\u7684\u53d1\u73b0\u5728\u672c\u5730\u91cd\u65b0\u7f16\u8bd1\u4e00\u6b21static lib\uff0c\u518d\u94fe\u63a5\u5c31\u4e00\u5207\u6b63\u5e38\u50da\u3002\u65e2\u7136\u8fd9\u6837\u80af\u5b9a\u662f\u7f16\u8bd1\u51fa\u6765\u7684asm\u6709\u95ee\u9898\uff0c\u7565\u53bb\u75db\u82e6\u7684\u8fc7\u7a0b\uff0c\u76f4\u63a5\u8fdb\u5165\u4e3b\u9898\uff0c\u4e00\u8def\u8ddf\u8fc7\u53bb\u53d1\u73b0\u4e86\u4e0d\u540c\u3002\u5728\u9519\u8bef\u7684lib\u4e2d\uff0cptr-&gt;fp\u7684\u5730\u5740\u662fptr+0x8h\uff0c\u800c\u6b63\u786e\u7684lib\u91cc\u9762\u662fptr+0x10h\u3002\u4e8e\u662f\u53bb\u770b\u4e86\u4e00\u4e0b\u6211\u672c\u5730\u7f16\u8bd1\u51fa\u6765\u7684sizeof(Callback)\uff0c\u731c\u731c\u662f\u591a\u5c11\uff1f \u7b54\u6848\u662f32\u3002 \u4e3a\u5565\u8fd9\u4e48\u5927\u554a\uff0c\u4e8e\u662f\u53c8\u6c42\u4e86\u4e0bsizeof(fp)\u3002\u731c\u731c\u770b\u8fd9\u6b21\u53c8\u662f\u591a\u5c11\uff1f \u7b54\u6848\u662f16\u3002 \u672c\u6765\u4ee5\u4e3afp\u81f3\u591a\u518d\u5b58\u4e2a\u865a\u8868\uff0c\u66f4\u4f55\u51b5\u6211\u8fd9\u4e2a\u662fnon-virtual\u7684\uff0c\u4e0d\u8981\u865a\u8868\uff0c\u7ed3\u679c\u6ca1\u60f3\u5230\u641e\u51fa\u8fd9\u4e48\u5927\u4e00\u5768\u3002\u8fd9\u6837\u770b\u6765\uff0c\u56e0\u4e3a\u8981\u5bf9\u9f50\u6240\u4ee5\u6574\u4e2aCallback\u7ed3\u6784\u4f53\u88ab\u641e\u6210\u4e8632\u5b57\u8282\uff0c\u8fd9\u4e2achar*\u53ef\u771f\u662f\u6d6a\u8d39\u5462\u3002\u7531\u6b64\u63a8\u51fa\u9519\u8bef\u7684lib\u91cc\u9762\u662f\u628afp\u5f53\u505a8byte\u5927\u5c0f\u6765\u50a8\u5b58\u7684\uff0c\u6240\u4ee5ptr-&gt;fp\u5c31\u662fptr+0x8h\u3002\uff08\u597d\u5427\uff0c\u8fd9\u4e00\u6bb5\u7684\u5206\u6790\u5728\u4e8b\u540e\u8bc1\u660e\u662f\u9519\u7684\uff0cfp\u603b\u662f16byte\uff0c\u51fa\u9519\u662f\u56e0\u4e3a\u9519\u8bef\u7684\u5bf9\u9f50\u89c4\u5219\uff0c\u524d\u8005\u9519\u8bef\u7684\u4f7f\u7528\u4e86#pragma pack(16)\u7684\u89c4\u5219\uff0c\u90aa\u6076\u7684vc6.0 sp5\u2026\u2026\uff09 \u6bd4\u8f83\u5de7\u5408\u7684\u662f\uff0c\u7531\u4e8e\u4f20\u5165\u7684Callback*\u6765\u81ea\u4e00\u4e2a\u50a8\u5b58\u5728mem pool\u91cc\u7684\u6570\u636e\uff0c\u800c\u8fd9\u4e2amem pool\u662f\u7531\u81ea\u5df1\u7684\u5185\u5b58\u5206\u914d\u5668\u6765\u7ba1\u7406\u7684\uff0c\u4e00\u5f00\u59cb\u4f1a\u628apool\u91cc\u7684\u6570\u636e\u5168\u90e8\u51990\uff0c\u6240\u4ee5\u9519\u8bef\u7684\u6307\u9488\u6070\u597d\u6307\u5411\u4e86\u4e00\u6bb5\u4e3a0\u7684\u5185\u5b58\uff0c\u9634\u5dee\u9633\u9519\u7684\u901a\u8fc7\u4e86if\u5224\u65ad\uff0c\u6240\u4ee5\u6ca1\u6709\u62a5access violation\u3002 \u6700\u91cd\u8981\u7684\u662f\u4e3a\u5565\u4f1a\u6709\u8fd9\u4e48\u5927\u7684fp\uff1f\u653e\u72d7\u67e5\u5230\u4e00\u7bc7\u6587\u7ae0\uff0c\u300aMember Function Pointers and the Fastest Possible C++ Delegates\u300b\uff0c\u539f\u6765\u8fd9\u4e48\u590d\u6742\u3002\u5176\u5b9e\u6211\u4ee5\u524d\u8fd8\u7528\u8fc7\u8fd9\u4e2afastdelegate\uff0c\u4f46\u662f\u5b8c\u5168\u6ca1\u6ce8\u610f\u5230\u6587\u7ae0\u4e2d\u6709\u4e2a\u63cf\u8ff0\u4e0d\u540c\u7f16\u8bd1\u5668\u4e0b\u7684member-function &hellip; <a href=\"https:\/\/blog.mega-zone.org\/?p=122\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;\u90aa\u6076\u7684member-function pointer&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[12],"_links":{"self":[{"href":"https:\/\/blog.mega-zone.org\/index.php?rest_route=\/wp\/v2\/posts\/122"}],"collection":[{"href":"https:\/\/blog.mega-zone.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mega-zone.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mega-zone.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mega-zone.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=122"}],"version-history":[{"count":9,"href":"https:\/\/blog.mega-zone.org\/index.php?rest_route=\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":139,"href":"https:\/\/blog.mega-zone.org\/index.php?rest_route=\/wp\/v2\/posts\/122\/revisions\/139"}],"wp:attachment":[{"href":"https:\/\/blog.mega-zone.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mega-zone.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mega-zone.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}