{"id":2276,"date":"2015-04-09T09:05:09","date_gmt":"2015-04-09T01:05:09","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=2276"},"modified":"2015-04-09T09:05:09","modified_gmt":"2015-04-09T01:05:09","slug":"%e8%af%91-%e5%a6%82%e4%bd%95%e5%9c%a8-c-builder-%e4%bd%bf%e7%94%a8-delphi-%e4%b8%ad%e7%9a%84%e6%b3%9b%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=2276","title":{"rendered":"[\u8bd1] \u5982\u4f55\u5728 C++ Builder \u4f7f\u7528 Delphi \u4e2d\u7684\u6cdb\u578b"},"content":{"rendered":"<p>\u2026\u2026\u7701\u7565\u5e9f\u8bdd\u82e5\u5e72\u2026\u2026<\/p>\n<p>Delphi\u7684\u6cdb\u578b\u5728C++\u4e2d\u88ab\u66b4\u9732\u4e3a\u6a21\u677f\u3002\u7136\u800c\u5f88\u91cd\u8981\u7684\u4e00\u70b9\u662f\u5b83\u7684\u771f\u6b63\u5b9e\u4f8b\u5316\u4e8e Delphi \u90a3\u8fb9\uff0c\u800c\u4e0d\u662fC++\u3002\u6240\u4ee5\uff0c\u4f60\u53ea\u80fd\u4f7f\u7528\u90a3\u4e9b Delphi \u4ee3\u7801\u4e2d\u5df2\u7ecf\u663e\u5f0f\u5b9e\u4f8b\u5316\u7684\u6a21\u677f\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a Delphi \u4e2d\u7684\u7b80\u5355\u6cdb\u578b TList&lt;T&gt;\uff1a<\/p>\n<pre class=\"lang:delphi decode:true \">unit DelphiUnit;\r\n \r\ninterface\r\n  uses System.Generics.Collections;\r\n \r\ntype\r\n  MyTList&lt;T&gt; = class(TList&lt;T&gt;)\r\n  public\r\n    \/\/ Anchors constructor\/destructor\r\n    constructor Create;\r\n    destructor Destroy; override;\r\n \r\n    class procedure Cleanup(var L: MyTList&lt;T&gt;); static;\r\n  end;\r\n \r\n  \/\/ DoubleList: instantiates MyTList&lt;double&gt;\r\n  DoubleList = class(MyTList&lt;double&gt;)\r\n  end;\r\n \r\n  \/\/ StringList: instantiates MyTList&lt;string&gt;\r\n  StringList = class(MyTList&lt;string&gt;)\r\n  end;\r\n \r\nimplementation\r\n \r\nclass procedure MyTList&lt;T&gt;.Cleanup(var L: MyTList&lt;T&gt;);\r\nbegin\r\n  L.Free;\r\nend;\r\n \r\nconstructor MyTList&lt;T&gt;.Create;\r\nbegin\r\n  inherited;\r\nend;\r\n \r\ndestructor MyTList&lt;T&gt;.Destroy;\r\nbegin\r\n  inherited;\r\nend;\r\n \r\nend.<\/pre>\n<p>\u4e0a\u9762\u7684\u63a5\u53e3\u5728 C++ \u4e2d\u5c06\u66b4\u9732\u4e3a\uff1a<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ CodeGear C++Builder\r\n\/\/ Copyright (c) 1995, 2012 by Embarcadero Technologies, Inc.\r\n\/\/ All rights reserved\r\n \r\n\/\/ (DO NOT EDIT: machine generated header) 'DelphiUnit.pas' rev: 24.00 (Windows)\r\n \r\n#ifndef DelphiunitHPP\r\n#define DelphiunitHPP\r\n \r\n#pragma delphiheader begin\r\n#pragma option push\r\n#pragma option -w       \/\/ Display all warnings\r\n#pragma option -w-inl   \/\/ Functions %s are not expanded inline\r\n#pragma option -w-8111  \/\/ Accessing deprecated entity\r\n#pragma option -Vx      \/\/ Zero-length empty class member\r\n#pragma pack(push,8)\r\n#include &lt;System.hpp&gt;\t\/\/ Pascal unit\r\n#include &lt;SysInit.hpp&gt;\t\/\/ Pascal unit\r\n#include &lt;System.Generics.Collections.hpp&gt;\t\/\/ Pascal unit\r\n#include &lt;System.Generics.Defaults.hpp&gt;\t\/\/ Pascal unit\r\n#include &lt;System.Types.hpp&gt;\t\/\/ Pascal unit\r\n \r\n\/\/-- user supplied -----------------------------------------------------------\r\n \r\nnamespace Delphiunit\r\n{\r\n\/\/-- type declarations -------------------------------------------------------\r\ntemplate&lt;typename T&gt; class DELPHICLASS MyTList__1;\r\n\/\/ Template declaration generated by Delphi parameterized types is\r\n\/\/ used only for accessing Delphi variables and fields.\r\n\/\/ Don't instantiate with new type parameters in user code.\r\ntemplate&lt;typename T&gt; class PASCALIMPLEMENTATION MyTList__1 : public System::Generics::Collections::TList__1&lt;T&gt;\r\n{\r\n\ttypedef System::Generics::Collections::TList__1&lt;T&gt; inherited;\r\n \r\npublic:\r\n\t__fastcall MyTList__1(void);\r\n\t__fastcall virtual ~MyTList__1(void);\r\n\tstatic void __fastcall Cleanup(MyTList__1&lt;T&gt;* &amp;L);\r\n};\r\n \r\n \r\nclass DELPHICLASS DoubleList;\r\nclass PASCALIMPLEMENTATION DoubleList : public MyTList__1&lt;double&gt;\r\n{\r\n\ttypedef MyTList__1&lt;double&gt; inherited;\r\n \r\npublic:\r\n\t\/* {DelphiUnit}MyTList&lt;System_Double&gt;.Create *\/ inline __fastcall DoubleList(void) : MyTList__1&lt;double&gt;() { }\r\n\t\/* {DelphiUnit}MyTList&lt;System_Double&gt;.Destroy *\/ inline __fastcall virtual ~DoubleList(void) { }\r\n \r\n};\r\n \r\n \r\nclass DELPHICLASS StringList;\r\nclass PASCALIMPLEMENTATION StringList : public MyTList__1&lt;System::UnicodeString&gt;\r\n{\r\n\ttypedef MyTList__1&lt;System::UnicodeString&gt; inherited;\r\n \r\npublic:\r\n\t\/* {DelphiUnit}MyTList&lt;System_string&gt;.Create *\/ inline __fastcall StringList(void) : MyTList__1&lt;System::UnicodeString&gt;() { }\r\n\t\/* {DelphiUnit}MyTList&lt;System_string&gt;.Destroy *\/ inline __fastcall virtual ~StringList(void) { }\r\n \r\n};\r\n \r\n \r\n\/\/-- var, const, procedure ---------------------------------------------------\r\n}\t\/* namespace Delphiunit *\/\r\n#if !defined(DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE) &amp;&amp; !defined(NO_USING_NAMESPACE_DELPHIUNIT)\r\nusing namespace Delphiunit;\r\n#endif\r\n#pragma pack(pop)\r\n#pragma option pop\r\n \r\n#pragma delphiheader end.\r\n\/\/-- end unit ----------------------------------------------------------------\r\n#endif\t\/\/ DelphiunitHPP<\/pre>\n<p>C++ \u4ee3\u7801\u94fe\u63a5\u4e0a\u9762\u7684 Delphi \u5355\u5143\u4ea7\u751f\u7684 .obj \u5c31\u53ef\u4ee5\u4f7f\u7528 <code>MyTList__1&lt;double&gt;<\/code>\u00a0\u6216\u00a0<code>MyTList__1&lt;System::String&gt; \u5b9e\u4f8b\u3002<\/code><\/p>\n<pre class=\"lang:delphi decode:true \">void UseDLists()\r\n{\r\n  \/\/ C++ code can use the Generics defined in Delphi directly\r\n  \/\/ as long as the C++ code limits itself to types for which\r\n  \/\/ the generic was instantiated on the Delphi side. For example,\r\n  \/\/ since the Delphi Unit instantiates MyTList&lt;String&gt;\r\n  \/\/ and MyTList&lt;double&gt; we can use these here.\r\n  \/\/ However, if we try to use MyTList__1&lt;char&gt; we'll get\r\n  \/\/ errors since the Delphi side did not instantiate\r\n  \/\/ MyTList&lt;AnsiChar&gt;.\r\n  MyTList__1&lt;double&gt;* dblList = new MyTList__1&lt;double&gt;();\r\n  dblList-&gt;Add(1.0);\r\n  dblList-&gt;Add(1.5);\r\n  double d = dblList-&gt;Items[1];\r\n#ifdef _WIN64\r\n  delete dblList\r\n#else\r\n  MyTList__1&lt;double&gt;::Cleanup(dblList);\r\n#endif\r\n \r\n  MyTList__1&lt;System::String&gt; *strList = new MyTList__1&lt;System::String&gt;();\r\n  strList-&gt;Add(\"First\");\r\n  strList-&gt;Add(\"Second\");\r\n  strList-&gt;Add(\"Third\");\r\n  assert(strList-&gt;Count == 3);\r\n \r\n  System::String str = strList-&gt;Items[0];\r\n  assert(str == \"First\");\r\n  assert(strList-&gt;Items[1] == \"Second\");\r\n  assert(strList-&gt;Items[2] == \"Third\");\r\n \r\n  strList-&gt;Insert(0, \"Inserted\");\r\n  assert(strList-&gt;Count == 4);\r\n  assert(strList-&gt;Items[0] == \"Inserted\");\r\n  assert(strList-&gt;Items[1] == \"First\");\r\n \r\n  strList-&gt;Reverse();\r\n  assert(strList-&gt;Items[0] == \"Third\");\r\n  assert(strList-&gt;Items[1] == \"Second\");\r\n  assert(strList-&gt;Items[2] == \"First\");\r\n  assert(strList-&gt;Items[3] == \"Inserted\");\r\n \r\n  assert(strList-&gt;Contains(\"Inserted\"));\r\n  assert(!strList-&gt;Contains(\"Not Inserted\"));\r\n \r\n  strList-&gt;Sort();\r\n  strList-&gt;Remove(\"Inserted\");\r\n \r\n  assert(strList-&gt;Items[0] == \"First\");\r\n  assert(strList-&gt;Items[1] == \"Second\");\r\n  assert(strList-&gt;Items[2] == \"Third\");\r\n \r\n#ifdef _WIN64\r\n  delete strList;\r\n#else\r\n  MyTList__1&lt;System::String&gt;::Cleanup(strList);\r\n#endif\r\n}<\/pre>\n<p>\u5982\u679c\u4f60\u7684 C++ \u4ee3\u7801\u5c1d\u8bd5\u4f7f\u7528\u4e00\u4e2a\u6ca1\u6709\u5b9e\u4f8b\u5316\u7684 Delphi \u6cdb\u578b\uff0c\u4f60\u5c06\u5728\u94fe\u63a5\u65f6\u5f97\u5230\u9519\u8bef\u63d0\u793a\u3002\u4f8b\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u5c1d\u8bd5\u4f7f\u7528Delphi \u4ee3\u7801\u4e2d\u6ca1\u6709\u660e\u786e\u5b9e\u4f8b\u5316\u4e3a \u00a0MyTList&lt;AnsiChar&gt; \u7684\u00a0\u00a0MyTList__1&lt;char&gt; :<\/p>\n<pre class=\"lang:c++ decode:true \">void UseListOfChar()\r\n{\r\n  MyTList__1&lt;char&gt;* charList = new MyTList__1&lt;char&gt;();\r\n  charList-&gt;Add('a');\r\n  \/\/ ...\r\n}<\/pre>\n<p>\u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u7f16\u8bd1\u540e\uff0c\u5728\u94fe\u63a5\u65f6\u5c06\u62a5\u544a\u4e0b\u9762\u7684\u9519\u8bef\uff1a<\/p>\n<pre class=\"lang:tex decode:true \">[ilink32 Error] Error: Unresolved external 'Delphiunit::MyTList__1&lt;char&gt;::' referenced from CPPUNIT.OBJ\r\n[ilink32 Error] Error: Unresolved external '__fastcall Delphiunit::MyTList__1&lt;char&gt;::MyTList__1&lt;char&gt;()' referenced from CPPUNIT.OBJ\r\n[ilink32 Error] Error: Unresolved external '__fastcall System::Generics::Collections::TList__1&lt;char&gt;::Add(const const char)' referenced from CPPUNIT.OBJ\r\n[ilink32 Error] Error: Unable to perform link<\/pre>\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5728 Delphi \u4e2d\u58f0\u660e\u7c7b\u578b TMyList&lt;AnsiChar&gt;\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u2026\u2026\u7701\u7565\u5e9f\u8bdd\u82e5\u5e72\u2026\u2026 Delphi\u7684\u6cdb\u578b [&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":[226,383],"class_list":["post-2276","post","type-post","status-publish","format-standard","hentry","category-c-builder","category-delphi","category-misc","tag-226","tag-383"],"views":3515,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/2276","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=2276"}],"version-history":[{"count":1,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/2276\/revisions"}],"predecessor-version":[{"id":2277,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/2276\/revisions\/2277"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}