delphi - Translation check from C++ to ObjectPascal which is better -
i translating c++ code objectpascal (first time), , c++ still new me.
the c++ header file looks this:
class renumfd { public: renumfd(int length); ~renumfd(); void compfd(double *buff); //...other public functions cut space private: void rmfd(int n, int isgn, double *a, int *ap, double *kw); //...other private functions cut space int _length; int *_ap; double *_kw; }
i have translated them way:
type trenumfd = class private _length: integer; _ap: pinteger; _kw: pdouble; procedure rmfd(n:integer; isgn:integer; var a:double; var ap:integer; var kw:double); //... other procedures cut space public constructor create(const length:integer); destructor destroy(); override; procedure compfd(var buff:double); end;
i read pointers used parameters in c++ should set var parameters in object pascal. correct, or should stick more literal translation (worried being bitten later).
also in c++ constructor there following code, unsure in 1 line.
renumfd::renumfd(int length) { _length = length; _ap = new int[2 + (1 << ((int)(log(2.0 + length) + 0.5) / 2))]; _ap[0] = 0; <-- setting pointer nil, or assigning 0? //... }
i unsure if should translate line in object pascal filling first element 0 or assigning nil:
_ap := allocmem(2 + (1 shl (trunc(ln(2.0 + length) + 0.5) / 2))) * sizeof(integer)); _ap := nil;
maybe trying hard guess intent?
the decision use var
or pointer depends on how going used, , whether can set nil
or not. typically var
better, pointer has uses. did not provide enough code show decision best.
as array allocation, dynamic array better choice allocmem()
:
type renumfd = class public constructor create(length: integer); //... private _ap: array of integer; //... end;
constructor renumfd.create(length: integer); begin setlength(_ap, 2 + (1 shl (trunc(log(2.0) + 0.5) div 2))); _ap[0] := 0; // <-- assigns 0 first integer in array //... end;
Comments
Post a Comment