|
Delphi explained for C++ (or other language) programmers
Delphi compared with other languages
* means partial support or something complementary
The compilers
The interaction between the user and Delphi compiler is totally different than what you are used with. The compiler will simple stop whenever it encounters a tiny error and it will refuse to move on until you fix it. The beauty is that in most cases it will precisely tell you what is wrong and where. It will even put the cursor not only on the line where the error is, but on the precise location of the error so all you have to do is to start typing what the compiler says you have to type to fix the error. For example if you are trying to do this s:= i, where s is a string and i an integer, the compiler will let you know that you cannot assign a number to a string. The Delphi compiler is a one step compiler and its speed is about 200000 lines of code per second.
Procedures and functions
Calling convention
All functions exported by the C version of Hardware ID Extractor DLL are using the "stdcall" convention. This is slow but ensures compatibility with all Windows based programming languages (Visual C, Visual Basic, etc). The Pascal version is using the standard Delphi convention (faster).
Longint = Same as Integer Char = Same as Byte PChar = null terminated string (equivalent to LPSTR). String = no equivalent!
See full list at the end of the page.
Warning: Strong typed language
Delphi is an extremely "strong typed" language. The compiler will always look over your shoulder and show an "Error - cannot compile bla bla bla" when you are trying to assign a char to byte. Well, you can do that, and all the stuff you used to do in C++ but you have to tell the compiler that you want to do it and you know what you are doing. So, as a C++ programmer you will do it a lot - probably. But after a while you will see that it can be done without typecasting. Actually, typecasting is rarely used in Delphi. The most common typecast performed is when you wan to display the value of a variable in a (text) message. Anyway, some functions in Delphi accept untyped parameters. For example the FreeAndNil(var Obj) procedure will accept any type of object.
Delphi strings
Delphi strings are really special! They are really complex data structures and there is nothing resembling Delphi strings in C++. Do not assign a Delphi String or ShortString to a PChar (char *)! A Delphi string has no trailing #0 character to indicate where they end. Instead there are few "hidden" bytes in from of the string which shows the length of the string and and a counter (reference counter) which shows how many variable are pointing to the string. Delphi silently and transparently manage these counters.
ShortString= It is an array of bytes with the first byte being the length of the string. This means that it cannot be longer than 255 chars.
Comments
Delphi code is commented with a double slash (like C++ comments), or embraced in { } or (* *). Example: // This is a comment {This is also a comment} (* This is a comment also *)
Other important things to know
* In Delphi all instructions (instructions not lines) must end with a column (;) Example: if x> 10 then inc(x);
* In Delphi NOTHING is case sensitive! So 'MyFunction()' is the same as 'myfunction()'.
* All classes start with T. Example: TObject, TStringList, etc.
* All variable MUST BE declared. The compiler will stop as soon as it encounters an undeclared variable. The compiler does not initialize the local variables. It will warn you if you interrogate the value of a variable without initializing it before.
* The = sign is used for comparison and := for assignment. Example: if x= 3 then x:= 4;
* Boolean operators are: NOT, OR, AND Example: if NOT SaveToFile AND (x>0) then Result:= false;
* Properties, methods, fields, of a class are accessed with a dot. Example: MyClass.Create; MyClass.DoStuff; MyClass.Free;
* For loops are not so flexible as in C++ but are much faster. You cannot assign a value to the iteration inside the loop Example: for i:= 1 to 100 do i:= 10 <--------- Compiler won't let you
Also, after the loop, the value of the iterator is unknown due to code optimization (induction): Example: for i:= 1 to 100 do stuff; if i= 100 then <--------- Never do this! Here, i is not guaranteed to be 100!
Classes, methods, constructors
The C++ "static member function" are called in Delphi "methods". Static methods are C++ ordinary member functions and in Java are called "final methods". By default all methods are static unless you explicitly declare it "virtual". Abstract methods are called "pure virtual methods" in C++.
Delphi constructors are more flexible than C++ constructors. Usually constructors are named Create, but you can define multiple constructors (or destructors) using the same name or different names. The objects are created from the top derived class down towards the parent class (the opposite way, compared with C++). The parent class is constructed only if necessary! A constructor can call another constructor. Initialization code can be placed in constructor of later in "AfterConstruction" when the object is fully created.
Pointers
In Delphi pointers can be used the same as in C++. However, pointers are extremely rarely used by Delphi programmers. They are helped also by the language which hides the pointers. Address of a variable/object is obtained using the @ operator. Life cannot exist without water but programming CAN exist without pointers! In Delphi if you are using explicit pointers you are either doing something over-the-top cool, either you are doing it wrong (probably the second). Example: var p: pointer; p:= @MyObject;
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||