Indexes |
Top Previous Next |
What is translated > Indexes
While in C++ all arrays are zero based, that means, that they start at the index null, in Delphi arrays with other lower bounds can be defined. For example:
TRangeArray = array [3..7] of Integer;
The C++ code which is generated from the Delphi source has to correct the indexes accordingly. Because Delphi strings are one based, corrections also have to be done, if the target string type is zero based. Additional corrections have to be done, if the directive ZEROBASEDSTRINGS is set on.
The strategy at the translation wit Delphi2Cpp II is, that all functions that often are used to calculate indexes, will have the same results in C++ that they had in Delphi, but as soon as these values are used to access arrays or strings, the values are corrected. Functions, which deliver string positions therefore have to be defined differently, depending on the chosen target string. If th target string is one based as in Delphi the function High e.g. would become to:
UnicodeString::size_type High(const UnicodeString& X) { return X.Length - 1; // 1 based }
If the target string is zero based the following definition has to be used:
UnicodeString::size_type High(const UnicodeString& X) { return X.size(); // 0 based }
The zero based High function doesn't deliver the highest index any more, but the same value as in Delphi. The corrections is done at the access of the stings, as can be seen in the example below:
var arr : TRangeArray; s : String; begin
for index := Low(arr) to High(arr) do writeln(arr[index]);
for index := Low(s) to High(s) do writeln(s[index]);
->
TRangeArray arr; String s; for(index = 3 /*# Low(arr) */; index <= 7 /*# High(arr) */; index++) { WriteLn(arr[index - 3]); } for(index = 1 /*# Low(s)*/; index <= High(s); index++) { WriteLn(s[index - 1]); }
However, if the Delphi code uses hard-coded values as in the following example, the translation will fail:
MyString := 'This is a string.'; if Pos('a', myString) = 9 do ...
->
myString = L"This is a string."; if(myString.find(L"a") == 9) // bug: myString.find(L"a") == 8 ...
|
This page belongs to the DelphiXE2Cpp11 Documentation |
DelphiXE2Cpp11 home Content |