简体   繁体   English

将代码从C ++转换为Delphi

[英]Translating code from C++ to Delphi

I have the next code in C++: 我在C ++中有下一个代码:

for (long i=0; i < num_iter ; i++)
{
    bp->bpgt(data[i%8], &data[i%8][3]);

    if( bp->mse(&data[i%8][3]) < thresh) 
        break;
}

where bpgt is a procedure, and mse is a function, thresh is a Double type, data is a bi-dimensional matrix of Double types. 其中bpgt是一个过程,而mse是一个函数,thresh是Double类型,data是Double类型的二维矩阵。

void bpgt(double *in,double *tgt);
double mse(double *tgt);
double data[][4]={
        0,0,0,0,
        0,0,1,1,
    1,1,1,1 };

I've tried to pass it to Delphi code: 我试过把它传递给Delphi代码:

for i := 0 to FNum_Iter - 1 do begin
    FBPN.bpgt(FData[i mod 8], ^FData[i mod 8,3]);

    if FBPN.mse(@FData[i mod 8, 3]) < FThresh then
      Break;
end;

but I've failed, because I'm a newbie in C++ and I dont know to translate the "&" operator. 但我失败了,因为我是C ++的新手,我不知道翻译“&”运算符。 May Someone help me? 愿有人帮助我吗?

Thanks in advance. 提前致谢。

I would translate 我会翻译

void bpgt(double *in,double *tgt);

as

procedure bpgt(var in:double; var tgt: double)

Well, something like that, my Delphi is a bit rusty.... 好吧,就像这样,我的德尔福有点生疏....

That way, in bpgt, you can change the value of tgt (and in). 这样,在bpgt中,您可以更改tgt(和in)的值。

Your call of bpgt will be 你的bpgt电话会是

FBPN.bpgt(FData[i mod 8], FData[i mod 8,3]);

In fact Delphi's var (call by reference) is quite often usable as an exact functional equivalent of C/C++'s passing of pointers. 事实上,Delphi的var(通过引用调用)通常可用作C / C ++指针传递的精确功能等价物。

This is based on your reply to Fvu's answer. 这是基于您对Fvu答案的回复。 If they're arrays, that can complicate things, since C doesn't have real arrays, just pointers sprinkled with a light coating of powdered syntactic sugar. 如果它们是阵列,那可能会使事情变得复杂,因为C没有真正的数组,只是指针上撒上一层粉末状的合成糖。 The solution depends on whether you're translating this completely into Delphi, or trying to write a Delphi routine that will work with C code. 解决方案取决于您是将其完全转换为Delphi,还是尝试编写可与C代码一起使用的Delphi例程。

If you're doing pure Delphi, declare an array type, like so 如果您正在使用纯Delphi,请声明一个数组类型,就像这样

type
   TDoubleArray = array[0..length] of double; //or "array of double" for a dynamic array

Then declare the parameter of the function as var Data: TDoubleArray . 然后将函数的参数声明为var Data: TDoubleArray If this is a dynamic array, have your for loop go from 0 to high(Data); 如果这是一个动态数组,那么你的for循环从0 to high(Data);

If this needs to work with C: 如果这需要与C一起使用:

Because C doesn't have real arrays, you need to do some extra work. 因为C没有真正的数组,所以你需要做一些额外的工作。 Declare your type like this: 声明你的类型如下:

type
   TCDoubleArray = array[0..65535] of double;
   PCDoubleArray = ^TCDoubleArray;

Any sufficiently large value will work for the upper bound of the array. 任何足够大的值都适用于数组的上限。 We're not going to use all of it anyway. 无论如何,我们不打算全部使用它。 It just has to be bigger than you'll ever use. 它必须比你用过的更大。 Make the parameter type a PCDoubleArray, which corresponds to double * . 使参数类型为PCDoubleArray,对应于double * You'll need to pass an extra parameter, which tells you where the end point of the array is. 您需要传递一个额外的参数,该参数告诉您数组的终点在哪里。 Have your for loop go from 0 to the end point, and access it by saying something like FData^[i mod 8] . 让你的for循环从0到结束点,并通过说FData^[i mod 8]类的东西来访问它。

The & allows you to send the address of the variable into the function, rather than it's value. &允许您将变量的地址发送到函数中,而不是它的值。 This call by reference allows the function to alter the value of the variable. 此引用调用允许函数更改变量的值。 I don't know the equivalent Delphi mechanism, but you want to pass the reference of your variable there, or change the procedure to a function, return the value and store it in the original variable. 我不知道等效的Delphi机制,但是你想在那里传递变量的引用,或者将过程改为函数,返回值并将其存储在原始变量中。

You are close, try this: 你很近,试试这个:

for i := 0 to FNum_Iter - 1 do begin
    FBPN.bpgt(FData[i mod 8], FData[i mod 8,3]);

    if FBPN.mse(FData[i mod 8, 3]) < FThresh then
      Break;
end;

Sorry, but I don't have delphi here to compile and try it by myself. 对不起,我在这里没有delphi编译并亲自试用。

Over at Rudy's Delphi Corner , he has an excellent article about the pitfalls of converting C/C++ to Delphi . Rudy的Delphi Corner ,他有一篇关于将C / C ++转换为Delphi的缺陷优秀文章 In my opinion, this is essential information when attempting this task. 在我看来,这是尝试此任务时的基本信息。 Here is the description: 这是描述:

This article is meant for everyone who needs to translate C/C++ headers to Delphi. 本文适用于需要将C / C ++头文件转换为Delphi的每个人。 I want to share some of the pitfalls you can encounter when converting from C or C++. 我想分享一下从C或C ++转换时可能遇到的一些陷阱。 This article is not a tutorial, just a discussion of frequently encountered problem cases. 本文不是教程,只是对经常遇到的问题案例的讨论。 It is meant for the beginner as well as for the more experienced translator of C and C++. 它适用于初学者以及更有经验的C和C ++翻译。

He also wrote a " Conversion Helper Package " that installs into the Delphi IDE which aids in converting C/C++ code to Delphi: 他还写了一个“ 转换助手包 ”,它安装在Delphi IDE中,有助于将C / C ++代码转换为Delphi:

替代文字
(source: rvelthuis.de ) (来源: rvelthuis.de

His other relevant articles on this topic include: 他关于这个主题的其他相关文章包括:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM