从C ++到Delphi的转换(简单) [英] Conversion from C++ to Delphi (simple)
问题描述
我在C ++中有一个函数,我试图在delphi中复制:
I have a function in C++ that I am attempting to replicate in delphi:
typedef double ANNcoord; // coordinate data type
typedef ANNcoord* ANNpoint; // a point
typedef ANNpoint* ANNpointArray; // an array of points
bool readPt(istream &in, ANNpoint p) // read point (false on EOF)
{
for (int i = 0; i < dim; i++) {
if(!(in >> p[i])) return false;
}
return true;
}
在Delphi中,我相信我已经正确地声明了数据类型。可能是错误的):
In Delphi I believe that I have correctly declared the data types.. (I could be wrong):
type
IPtr = ^IStream; // pointer to Istream
ANNcoord = Double;
ANNpoint = ^ANNcoord;
function readPt(inpt: IPtr; p: ANNpoint): boolean;
var
i: integer;
begin
for i := 0 to dim do
begin
end;
end;
但我不知道如何模仿C ++函数的行为(可能是因为我不明白位移算子)。
But I cannot figure out how to mimic the behavior in the C++ function (probably because I do not understand the bitshift operator).
此外,我需要最终找出如何将一组点从Zeos TZQuery
对象传输到相同的数据类型 - 所以如果任何人有一些任何输入,我会真的很感激。
Also, I need to eventually figure out how to transfer the set of points from a Zeos TZQuery
object to the same datatype - so if anyone has some any input on that I would really appreciate it.
推荐答案
尝试:
type
ANNcoord = Double;
ANNpoint = ^ANNcoord;
function readPt(inStr: TStream; p: ANNpoint): boolean;
var
Size: Integer; // number of bytes to read
begin
Size := SizeOf(ANNcoord) * dim;
Result := inStr.Read(p^, Size) = Size;
end;
不需要分别阅读每个ANNcoord。注意,istream是一个流类,而不是一个IStream接口,在C + +。 Delphi的等价是TStream。代码假定流被打开以进行读取(使用合适的参数创建Create-d),并且当前流指针指向一个数字(dim)的ANNcoords,就像C ++代码一样。
There is no need to read each ANNcoord separately. Note that istream is a stream class, not an IStream interface, in C++. Delphi's equivalent is TStream. The code assumes the stream is opened for reading (Create-d with the proper parameters) and the current stream pointer points to a number (dim) of ANNcoords, just like the C++ code does.
FWIW 在>> p [i]
从输入流中读取
到<$ c $>中的
,将 ANNcoord
c> p [i] p
解释为指向数组 ANNcoords
FWIW in >> p[i]
reads an ANNcoord
from the input stream in
to p[i]
, interpreting p
as a pointer to an array of ANNcoords
.
Rob Kennedy指出, myDouble
从输入流读取一个double,但是流被解释为 流,而不是二进制,即:
As Rob Kennedy pointed out, in >> myDouble
reads a double from the input stream, but the stream is interpreted as text stream, not binary, i.e. it looks like:
1.345 3.56845 2.452345
3.234 5.141 3.512
7.81234 2.4123 514.1234
etc...
AFAIK在Delphi中没有类似的方法或操作。为此目的只有 System.Read
和 System.Readln
。显然, Peter Below 曾写过一个单元StreamIO
,可以使用 System.Read
和 System.Readln
。我只能找到一个版本,在新闻组帖子。
There is, AFAIK, no equivalent method or operation in Delphi for streams. There is only System.Read
and System.Readln
for this purpose. Apparently Peter Below once wrote a unit StreamIO
which makes it possible to use System.Read
and System.Readln
for streams. I could only find one version, in a newsgroup post.
对于可以从其文本表示中读取双精度,整数,单数等的流,编写一个包装器可能是有意义的。我还没有看到。
It would probably make sense to write a wrapper for streams that can read doubles, integers, singles, etc. from their text representations. I haven't seen one yet.
这篇关于从C ++到Delphi的转换(简单)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!