奇怪? 是delphi的bug ?(50分)

  • 主题发起人 主题发起人 sanrex
  • 开始时间 开始时间
S

sanrex

Unregistered / Unconfirmed
GUEST, unregistred user!
这个程序没有问题
-------------
type
tt = array of char
implementation

{$R *.DFM}
procedure t(a : tt);
begin
end;
procedure TForm1.Button1Click(Sender: TObject);
var
f : tt;
begin
setlength(f,2);
f[0]:='1'
f[1]:='1'
t(f);
end;
---------
但是
type
tt = array of char
implementation

{$R *.DFM}
procedure t(a : array of char );
begin
end;
procedure TForm1.Button1Click(Sender: TObject);
var
f : array of char
begin
setlength(f,2);
f[0]:='1'
f[1]:='1'
t(f);
end;
就会报错:
[Error] Unit1.pas(36): Incompatible types: 'Array' and 'dynamic array'
 
不是Delphi的Bug,是你程序的Bug[:D]
 
Help->delphi Help
查Open array parameters
 
这不是Delphi的Bug,请注意Delphi的帮助如下:

Open array parameters allow arrays of different sizes to be passed to the same procedure or function. To define a routine with an open array parameter, use the syntax array of type (rather than array[X..Y] of type) in the parameter declaration. For example,
function Find(A: array of Char): Integer;
declares a function called Find that takes a character array of any size and returns an integer.
Note: The syntax of open array parameters resembles that of dynamic array types, but they do not mean the same thing. The example above creates a function that takes any array of Char elements, including (but not limited to) dynamic arrays. To declare parameters that must be dynamic arrays, you need to specify a type identifier:
type TDynamicCharArray = array of Char;
function Find(A: TDynamicCharArray): Integer;
For information about dynamic arrays, see Dynamic arrays.
Within the body of a routine, open array parameters are governed by the following rules.
1,They are always zero-based. The first element is 0, the second element is 1, and so forth. The standard Low and High functions return 0 and Length ?1, respectively. The SizeOf function returns the size of the actual array passed to the routine.
2,They can be accessed by element only. Assignments to an entire open array parameter are not allowed.
3,They can be passed to other procedures and functions only as open array parameters or untyped var parameters. They cannot be passed to SetLength.
4,Instead of an array, you can pass a variable of the open array parameter抯 base type. It will be treated as an array of length 1.

请注意上面的第3条
 
谢谢各位关注, 但是我认为你们没有仔细看看我的内容:

---我发现给函数t传递array of char 要出错,
[Error] Unit1.pas(36): Incompatible types: 'Array' and 'dynamic array'
---但是传递array of integer 或者 array of string 就不会
---另外如果我首先定义type f = array of char ,然后传递 f 类型参数就不会出错.
: Kang,
你让我查询帮助open array ,可dynamic array 和 open array并不是一个概念.

:tseug
如何解释array of char 出错,而array of string 不出错 ?

: sxbrf,
They can be passed to other procedures and functions only as open
array parameters or untyped var parameters.
我没有使用open array ,和 untyped var parameters,但是也可以传递到函数内

 
不如大家测试以下我的代码,也许是我的delphi特别一些那
 
是相容还是叫什么的,是PASCAL时的概念了,记不太清了,只会用了。。。
 
其實你第二個程序定義的是兩個類型,一個是TT型一個是Array of char,所以會出現錯誤
 
恩,我也认为是类型不相容引起的

虽然表面上是一样的,但是对于pascal语言来讲是不合法的

教科书上有论述的,你查查
 
Note: The syntax of open array parameters resembles that of dynamic array types,
but they do not mean the same thing. The example above creates a function
that takes any array of Char elements, including (but not limited to) dynamic arrays.
To declare parameters that must be dynamic arrays, you need to specify a type identifier
从这段话中,我们可以看到
procedure t(a:array of Char)中的array of Char不是动态数组的定义,
但它可以接受Char数组类型的变量,包括动态数组。如果你要限定只能传递动态数组,
那么你就要定义type TDynamicCharArray = array of Char;
但事实上,procedure t(a:array of Char/widechar)确实不接受动态数组。
而procedure t(a:array of integer/pchar/string)可以。
所以我认为是帮助文档与代码不统一,是DELPHI的一个BUG
考虑到CHAR、WIDECHAR有问题,可能是PCHAR、PWIDECHAR的定义带来的
动态数组是内存自管理的,而PCHAR、PWIDECHAR为了兼容,所以还是程序自己管理的
可能两者之间有冲突。要不就是编译器的问题。
 
这是Delphi的类型匹配太过严格的缘故,你如认为是bug也未尝不可。轻看下面:
aa:array of char;
bb:array of char;
aa:=bb;//error:Incompatible types
---------------------------------------
aa,bb:array of char;
aa:=bb;//通过
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
688
SUNSTONE的Delphi笔记
S
S
回复
0
查看
683
SUNSTONE的Delphi笔记
S
I
回复
0
查看
686
import
I
后退
顶部