delphi open arrays和dynamic arrays区别

来源:互联网 发布:c语言求100以内的质数 编辑:程序博客网 时间:2024/06/07 05:52

最近在写一个应用程序的时候遇到如下问题(由于与下面问题类似,直接转帖:)

================================================

I'm trying to resize an array of a certain class passed as an argument, e.g.

However, this raises an error "E2008 Incompatible types".

=================================================

产生此问题的原因主要为此处的Resize函数既可以接收静态数组参数,也可以接收动态数据参数,故SetLength方法不可使用。

解决方法为:重新定义数组类型,如:

================================================

================================================

以上方法告诉编译器传入的为动态数组,此时将可编译。

参考资料:

<!-- /* Font Definitions */ @font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;}@font-face{font-family:"/@宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:12.0pt;font-family:"Times New Roman";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;}h3{mso-margin-top-alt:auto;margin-right:0cm;mso-margin-bottom-alt:auto;margin-left:0cm;mso-pagination:widow-orphan;mso-outline-level:3;font-size:13.5pt;font-family:宋体;mso-bidi-font-family:宋体;font-weight:bold;}p{mso-margin-top-alt:auto;margin-right:0cm;mso-margin-bottom-alt:auto;margin-left:0cm;mso-pagination:widow-orphan;font-size:12.0pt;font-family:宋体;mso-bidi-font-family:宋体;}code{mso-ansi-font-size:12.0pt;mso-bidi-font-size:12.0pt;font-family:宋体;mso-ascii-font-family:宋体;mso-fareast-font-family:宋体;mso-hansi-font-family:宋体;mso-bidi-font-family:宋体;}pre{margin:0cm;margin-bottom:.0001pt;mso-pagination:widow-orphan;tab-stops:45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;font-size:12.0pt;font-family:宋体;mso-bidi-font-family:宋体;} /* Page Definitions */ @page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}@page Section1{size:595.3pt 841.9pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;mso-header-margin:42.55pt;mso-footer-margin:49.6pt;mso-paper-source:0;layout-grid:15.6pt;}div.Section1{page:Section1;}-->

Confusion

Although the syntax is unfortunately very similar, an open array parametershould not be confused with a Delphi dynamic array . Adynamic array is an array that is maintained by Delphi,and of which you can change the size using SetLength . It is declared like:

type TIntegerArray = array of Integer;

Unfortunately, this looks a lot like the syntax used foropen array parameters. But they are not the same . An open array parameter will acceptdynamic arrays like array of Month , but also static arrays like array[0..11]of Month . So in a function with an open array parameter, you can't callSetLengthon the parameter. If you really only want to pass dynamic arrays, you'll haveto declare them separately, and use the type name as parameter type.

  type
TMonthArray = array of Month;  
 
procedure AllKinds(const Arr: array of Month);
procedure OnlyDyn(Arr: TMonthArray);
Procedure AllKinds will accept static arrays as well as dynamic arrays, 
so SetLength can't be used, since static arrays can't be reallocated. Procedure OnlyDyn will only accept dynamic arrays, so you can 
use SetLength here (this will however use a copy, and not change the original array; if you want to change the length of the original array, 
use var Arr: TMonthArray in the declaration).