{"id":925,"date":"2014-10-16T16:29:40","date_gmt":"2014-10-16T08:29:40","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=925"},"modified":"2014-10-16T19:39:10","modified_gmt":"2014-10-16T11:39:10","slug":"delphi-c-%e5%9f%ba%e6%9c%ac%e8%af%ad%e6%b3%95%e5%af%b9%e7%85%a7%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=925","title":{"rendered":"Delphi <-> C++ \u57fa\u672c\u8bed\u6cd5\u5bf9\u7167\u8868"},"content":{"rendered":"<p>\u4e00\u3001\u6807\u5fd7\u7b26\u547d\u540d\uff1a\u4e24\u4e2a\u57fa\u672c\u4e00\u6837\uff0c\u9664\u4e86\u4fdd\u7559\u7684\u5173\u952e\u5b57\uff0cC\/C++\u533a\u5206\u5927\u5c0f\u5199\u5916\uff0c\u5269\u4e0b\u7684\u57fa\u672c\u4e00\u6837\u3002\u4e00\u822c\u6765\u8bf4\u4f60\u53ef\u4ee5\u7528Delphi\u7684\u4e60\u60ef\uff0c\u4f46\u6ce8\u610f\u5927\u5c0f\u5199\u5c31\u597d\u3002<\/p>\n<p>\u4e8c\u3001\u8fd0\u7b97\u7b26\uff1a<\/p>\n<p>\u6570\u5b66\u8fd0\u7b97\u7b26\uff1a+\u3001-\u3001*\u662f\u4e00\u6837\u7684\uff0c\u9664\u5728C++\u91cc\u5c31\u662f\u4e00\u4e2a\/\uff0c\u4e0d\u5206div\u548c\/\uff0c\u4e24\u4e2a\u6574\u6570\u76f4\u63a5\u5c31\u662f\u76f8\u5f53\u4e8ediv\uff0c\u4efb\u610f\u4e00\u4e2a\u662f\u6d6e\u70b9\u6570\u5c31\u662f\u6d6e\u70b9\u6570\u3002<\/p>\n<p>\u6bd4\u8f83\u64cd\u4f5c\u7b26\uff1a\u8fd9\u4e2aC\/C++\u7684\u4e0d\u7b49\u4e8e\u662f!=\uff0c\u800cDelphi\u662f&lt;&gt;<\/p>\n<p>\u4f4d\u8fd0\u7b97\u7b26\uff08C++ =&gt;\u00a0Delphi\uff0c\u4e0b\u540c\uff09\uff1a\u4f4d\u4e0e\uff08&amp; =&gt; and\uff09\u3001\u4f4d\u6216\uff08| =&gt; or\uff09\u3001\u4f4d\u975e\uff08~ =&gt; not\uff09\u3001\u5f02\u6216\uff08^ =&gt; xor\uff09\uff0c\u5de6\u79fb\uff08\u53d8\u91cf&lt;&lt;\u4f4d\u6570 =&gt;\u53d8\u91cf shl \u4f4d\u6570\uff09\u3001\u53f3\u79fb\uff08\u53d8\u91cf&gt;&gt;\u4f4d\u6570 =&gt; \u53d8\u91cf shr \u4f4d\u6570\uff09<\/p>\n<p>\u903b\u8f91\u8fd0\u7b97\u7b26\uff1a\u4e0e\uff08&amp;&amp; =&gt; and\uff09\u3001\u6216\uff08|| =&gt; or\uff09\u3001\u975e\uff08! =&gt; not\uff09<\/p>\n<p>\u5730\u5740\u8fd0\u7b97\u7b26\uff1a&amp; =&gt; @<\/p>\n<p>\u6210\u5458\u8fd0\u7b97\u7b26\uff1aDelphi\u5747\u4e3a\u201c.\u201d\uff0cC++\u4e2d\u6307\u9488\u5b9e\u4f8b\u7684\u6210\u5458\u7528 -&gt; \uff0c\u975e\u6307\u9488\u5b9e\u4f8b\u7684\u6210\u5458\u7528\u201c.\u201d\u3002<\/p>\n<p>\u6307\u9488\u5185\u5bb9\u64cd\u4f5c\u7b26\uff1a*\u6307\u9488 =&gt; \u6307\u9488^<\/p>\n<p>\u81ea\u8fd0\u7b97\u7b26\uff1a\u53d8\u91cf++ =&gt;Inc(\u53d8\u91cf)\uff0c++\u6574\u578b\u53d8\u91cf =&gt; \u65e0\uff0c\u53d8\u91cf&#8211; =&gt; Dec(\u53d8\u91cf)\uff0c\u53d8\u91cf+=\u503c =&gt; \u53d8\u91cf:=\u53d8\u91cf+\u503c\uff0c\u53d8\u91cf*=\u503c =&gt;\u53d8\u91cf:=\u53d8\u91cf*\u503c\uff0c\u4f9d\u6b21\u7c7b\u63a8\uff0cDelphi\u5269\u4e0b\u7684\u90fd\u6ca1\u6709<\/p>\n<p>\u3010\u6ce8\u3011\u5728C++\u4e2d\uff0c&amp;\u540c\u65f6\u88ab\u5f53\u4f5c\u5f15\u7528\u7b26\u53f7\uff0c\u5982:int &amp;a=b \uff0c\u6b64\u65f6\u8bbf\u95eea\u7684\u5185\u5bb9\u548c\u8bbf\u95eeb\u7684\u5185\u5bb9\u662f\u7b49\u4ef7\u7684\u3002<\/p>\n<p>\u4e09\u3001\u8bed\u53e5<\/p>\n<p>\u8bed\u53e5\u90fd\u662f\u4ee5\u201c;\u201d\u7ed3\u675f\uff0c\u590d\u5408\u8bed\u53e5\u5728C++\u4e2d\u662f\u7528\u5927\u62ec\u53f7\u5305\u542b\u5728\u4e00\u8d77\uff0cDelphi\u662fbegin\/end\uff0c\u5bf9\u5e94\u5173\u7cfb\uff1a<\/p>\n<p>{ =&gt; begin<\/p>\n<p>} =&gt; end<\/p>\n<p>\u56db\u3001\u53d8\u91cf\u58f0\u660e<\/p>\n<p>C++\u4e2d\u53d8\u91cf\u58f0\u660e\u7684\u4f4d\u7f6e\u4e0d\u5fc5\u4e00\u5b9a\u5728\u5f00\u59cb\uff0c\u4f46C\u7684\u8bdd\u89c4\u77e9\u5c31\u548cDelphi\u4e00\u6837\u4e86\u3002\u683c\u5f0f\uff1a<\/p>\n<p>\u7c7b\u578b \u53d8\u91cf\u540d; =&gt; var \u53d8\u91cf\u540d:\u7c7b\u578b<\/p>\n<p>\u5e38\u91cf\u7684C++\u548cDelphi\u4e00\u6837\u90fd\u662f\u7528const\u4fee\u9970\uff0c\u540c\u6837\uff0cC++\u7684\u53d8\u91cf\u548c\u5e38\u91cf\u5b9a\u4e49\u4f60\u53ef\u4ee5\u968f\u4fbf\u5728\u4ec0\u4e48\u5730\u65b9\u3002<\/p>\n<p>\u4e94\u3001\u6d41\u7a0b\u63a7\u5236<\/p>\n<p>\u6761\u4ef6\u5224\u65ad\uff1aif(\u6761\u4ef6)\/else if(\u6761\u4ef6)\/else =&gt;if \u00a0\u6761\u4ef6 then\/else if \u6761\u4ef6 then\/else<\/p>\n<p>\u5206\u652f\u9009\u62e9\uff1aswitch(\u53d8\u91cf){ case \u503c:\u8bed\u53e5;default:\u9ed8\u8ba4\u5904\u7406\u8bed\u53e5;}=&gt;case \u53d8\u91cf of \u503c:\u8bed\u53e5 else \u9ed8\u8ba4\u8bed\u53e5 end<\/p>\n<p>while\u5faa\u73af\uff1awhile(\u6761\u4ef6)\u8bed\u53e5; =&gt; while \u6761\u4ef6 do \u8bed\u53e5;<\/p>\n<p>repeat\u5faa\u73af\uff1ado \u8bed\u53e5 while(\u6761\u4ef6); =&gt; repeat \u8bed\u53e5 until \u6761\u4ef6;\u6ce8\u610f\u4e24\u4e2a\u6761\u4ef6\u662f\u76f8\u53cd\u7684\uff0c\u4e00\u4e2a\u662f\u6210\u7acb\u65f6\u7ee7\u7eed\uff0c\u4e00\u4e2a\u662f\u76f4\u5230\u4e0d\u6210\u7acb<\/p>\n<p>for\u5faa\u73af\uff1afor(\u521d\u59cb\u5316\u8bed\u53e5;\u7ed3\u675f\u6761\u4ef6;\u989d\u5916\u63a7\u5236) \u8bed\u53e5 =&gt; for \u5faa\u73af\u53d8\u91cf:=\u521d\u59cb\u503c to\/downto \u76ee\u6807\u503c do \u8bed\u53e5<\/p>\n<p>\u516d\u3001\u51fd\u6570<\/p>\n<p>C++\u4e0d\u533a\u5206\u8fc7\u7a0b\u548c\u51fd\u6570\uff0c\u8fc7\u7a0b\u5bf9\u4e8eC++\u800c\u8a00\u53ea\u662f\u8fd4\u56de\u503c\u4e3avoid\u7684\u51fd\u6570\uff0cDelphi\u4e2d\u51fd\u6570\u662f\u8981\u6c42\u6709\u8fd4\u56de\u503c\uff0c\u8fc7\u7a0b\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u5bf9\u5e94\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<p>\u51fd\u6570\uff1a<\/p>\n<p>\u8fd4\u56de\u503c\u7c7b\u578b [\u4fee\u9970\u7b26] \u51fd\u6570\u540d(\u51fd\u6570\u53c2\u6570){\u51fd\u6570\u4ee3\u7801} =&gt; function \u51fd\u6570\u540d(\u51fd\u6570\u53c2\u6570):\u8fd4\u56de\u503c\u7c7b\u578b;[\u4fee\u9970\u7b26];begin \u51fd\u6570\u4ee3\u7801 end;<\/p>\n<p>\u8fc7\u7a0b\uff1a<\/p>\n<p>void\u00a0 [\u4fee\u9970\u7b26] \u51fd\u6570\u540d(\u51fd\u6570\u53c2\u6570){\u51fd\u6570\u4ee3\u7801} =&gt; procedure\u51fd\u6570\u540d(\u51fd\u6570\u53c2\u6570):\u8fd4\u56de\u503c\u7c7b\u578b;[\u4fee\u9970\u7b26];begin \u51fd\u6570\u4ee3\u7801 end;<\/p>\n<p>\u3010\u6ce8\u3011<\/p>\n<p>\u5982\u679c\u51fd\u6570\u53c2\u6570\u4f20\u5730\u5740\uff0cDelphi\u7684var\u7c7b\u578b\u5b9a\u4e49\uff0c\u4e00\u822c\u7ffb\u8bd1\u6210\u5f15\u7528\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u7ffb\u8bd1\u6210\u6307\u9488\u3002\u5982\uff1a<\/p>\n<pre class=\"lang:delphi decode:true \">procedure Inc1(var x:Integer);\r\nbegin\r\nInc(x);\r\nend;<\/pre>\n<p>\u7ffb\u8bd1\u6210C++\u53ef\u4ee5\u662f\uff1a<\/p>\n<pre class=\"lang:delphi decode:true \">void __fastcall Inc1(int &amp;x)\r\n{\r\nx++;\r\n}\r\n\u6216\r\nvoid __fastcall Inc11(int *x)\r\n{\r\n(*x)++;\r\n}<\/pre>\n<p>\u52a0\u4e0a__fastcall\u4fee\u9970\u7b26\u662f\u56e0\u4e3aDelphi\u7684\u8c03\u7528\u89c4\u5219\u9ed8\u8ba4\u662ffastcall\uff0c\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<p>\u4e03\u3001\u57fa\u672c\u7c7b\u578b<\/p>\n<p>C++\u6ca1\u6709\u96c6\u5408\u7c7b\u578b\uff0c\u6240\u4ee5delphi\u4e2d\u7684set\u6ca1\u6709\u76f4\u63a5\u5bf9\u5e94\u7c7b\u578b\uff0c\u4f46C++ Builder\u5bf9\u5e94\u4e86\u4e00\u4e2aSet\u6a21\u677f\u7c7b\uff0c\u54b1\u4eec\u4e0d\u8bf4\u3002\u53ea\u8bf4\u57fa\u672c\u7c7b\u578b\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\uff1a<\/p>\n<p>char =&gt; AnsiChar<\/p>\n<p>wchar_t =&gt; WideChar<\/p>\n<p>unsigned char =&gt; Byte<\/p>\n<p>short =&gt; Smallint<\/p>\n<p>unsigned short =&gt; Word<\/p>\n<p>int =&gt; integer<\/p>\n<p>unsigned int =&gt;Cardinal<\/p>\n<p>long =&gt; Longint<\/p>\n<p>unsigned long =&gt;Cardinal<\/p>\n<p>__int64 =&gt; Int64<\/p>\n<p>unsigned __int64 =&gt;UInt64<\/p>\n<p>enum \u679a\u4e3e\u7c7b\u578b\u540d {\u679a\u4e3e\u503c\u5217\u8868 } =&gt; type \u679a\u4e3e\u7c7b\u578b\u540d=(\u679a\u4e3e\u503c\u5217\u8868\uff09<\/p>\n<p>struct \u7ed3\u6784\u4f53\u7c7b\u578b\u540d{\u6210\u5458\u5b9a\u4e49}=&gt; \u7ed3\u6784\u4f53\u7c7b\u578b\u540d=[packed] record \u6210\u5458\u5b9a\u4e49 end;<\/p>\n<p>class \u7c7b\u578b\u540d[: public\/protected\/private \u7236\u7c7b\u540d]=&gt; \u7c7b\u578b\u540d=class[(\u7236\u7c7b\u540d)]<\/p>\n<p>union \u8054\u5408\uff08\u5171\u7528\u4f53\uff09\u7c7b\u578b\u540d{\u6210\u5458\u5b9a\u4e49} =&gt; \u7c7b\u578b\u540d=record case \u7c7b\u578b of \u00a0\u503c:(\u6210\u5458\u5b9a\u4e49); end;<\/p>\n<p>\u4f4d\u57df\u5728Delphi\u4e2d\u6ca1\u6709\u5bf9\u5e94\u7684\u5b9a\u4e49\uff0c\u53ea\u80fd\u5b9a\u4e49\u4e3a\u76f8\u5e94\u7684\u7c7b\u578b\uff0c\u7136\u540e\u81ea\u5df1\u53bb\u68c0\u6d4b\u6807\u5fd7\u4f4d\u3002<\/p>\n<p>\u3010\u6ce8\u3011C++\u4e2d\uff0c\u7ed3\u6784\u3001\u7c7b\u548c\u63a5\u53e3\u5b9e\u9645\u4e0a\u6ca1\u6709\u4ec0\u4e48\u672c\u8d28\u7684\u533a\u522b\uff0cC++\u7684\u7ed3\u6784\u4f53\u4e00\u6837\u53ef\u4ee5\u6709\u6784\u9020\u548c\u6790\u6784\u51fd\u6570\uff0c\u4e00\u6837\u53ef\u4ee5\u7ee7\u627f\u548c\u91cd\u8f7d\u51fd\u6570\uff0c\u5982\u679c\u975e\u8981\u8bf4\u6709\u533a\u522b\uff0c\u90a3\u4e48\u6700\u5927\u7684\u533a\u522b\u5c31\u662fC++\u91cc\u7ed3\u6784\u4f53\u9ed8\u8ba4\u6210\u5458\u662f\u516c\u5f00\u7684(public)\uff0c\u800c\u7c7b\u9ed8\u8ba4\u662f\u79c1\u6709\u7684(private)\uff0c\u4f46\u4f60\u53ef\u4ee5\u7528\u5b8c\u5168\u540c\u6837\u7684\u8bed\u6cd5\u53bb\u89c4\u5b9a\u5404\u4e2a\u6210\u5458\u7684\u53ef\u89c1\u6027\u3002\u81f3\u4e8e\u63a5\u53e3\uff0cC++\u4e2d\u672c\u8eab\u5c31\u662frecord\u7684\u522b\u540d\u3002<\/p>\n<p>\u516b\u3001\u5f15\u7528\u5916\u90e8\u6587\u4ef6<\/p>\n<p>#include &lt;\u6587\u4ef6\u540d&gt; =&gt;uses \u5355\u5143\u540d \u6216 {$I \u6587\u4ef6\u540d}<\/p>\n<p>#include &#8220;\u6587\u4ef6\u540d&#8221; =&gt; uses \u5355\u5143\u540d \u6216 {$I \u6587\u4ef6\u540d}<\/p>\n<p>\u4e5d\u3001\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362<\/p>\n<p>(\u7c7b\u578b)\u503c =&gt; \u7c7b\u578b(\u503c)<\/p>\n<p>dynamic_cast&lt;\u7c7b\u578b&gt;(\u53d8\u91cf) =&gt; \u503c as \u7c7b\u578b<\/p>\n<p>Delphi\u7684is\u53ef\u4ee5\u7528\u68c0\u67e5dynamic_cast\u7684\u503c\u662f\u5426\u4e3a\u7a7a\u6765\u4ee3\u66ff\uff0c\u53e6\u5916\uff0cC++\u8fd8\u6709\u4e00\u5806static_cast\/reinterpret_cast\/const_cast\u7b49\u8f6c\u6362\u65b9\u5f0f\uff0c\u4f46\u5e38\u7528\u7684\u662f\u4e0a\u9762\u7684\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6807\u5fd7\u7b26\u547d\u540d\uff1a\u4e24\u4e2a\u57fa\u672c\u4e00\u6837\uff0c\u9664\u4e86\u4fdd\u7559\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[69,8,21],"tags":[31,450,120,119],"class_list":["post-925","post","type-post","status-publish","format-standard","hentry","category-c-builder","category-delphi","category-misc","tag-c","tag-delphi","tag-120","tag-119"],"views":7103,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=925"}],"version-history":[{"count":10,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/925\/revisions"}],"predecessor-version":[{"id":935,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/925\/revisions\/935"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}