Free desktop tools and utilities for Windows. Download our software now.Free desktop tools and utilities for Windows. Download our software now.
HomeContact
molecular biology software
scf trace assembly

Delphi explained for C++ (or other language) programmers
A quick guide

Delphi programming C/C++  programming VB (Visual Basic)  programming

 


 

Delphi compared with other languages

 

Language Feature

Delphi

Java

C++

Visual Basic

Multiple inheritance

 

 

v

 

Operator overloading

 

 

v

 

Generic types (templates)

v

 

v

 

Class (static) fields

v

v

v

 

Dynamic methods

v

 

   

Message passing

v

     

Built-in assembler

v

 

*

 

Inline functions

v

 

v

 

Non-class functions

v

 

v

v

Non-object variables

v

 

v

v

Garbage collection

*

v

 

*

Variant types

v

 

 

v

OLE automation

v

 

 

v

Properties

v

 

 

v

Single root class

v

v

 

 

Metaclasses

v

v

 

 

Built-in support for threads

v

v

   

Runtime type information

v

v

*

 

Static type-checking

v

v

v

 

Function overloading

v

v

v

 

Virtual methods

v

v

v

 

Class (static) methods

v

v

v

 

Inheritance

v

v

v

 

Abstract (pure) virtual methods

v

v

v

 

Exception handling

v

v

v

v

Interfaces

v

v

*

v

 

* 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


In Delphi, a procedure is a function that returns nothing. Because of the calling convention used, calling a procedure is much faster than calling a function. Functions that take no parameters DO NOT have to be followed by parenthesis. MyFunction is recommended over MyFunction().

 

 

Calling convention


The calling convention in Delphi is FastCall (also called Register). If FastCall, evaluating arguments is done from left to right, it passes three arguments via EAX, EDX, ECX. Remaining arguments are pushed onto the stack, also left to right.

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).

 


Types of variables commonly used in Delphi:


Cardinal = 32 bits unsigned
Integer = 32 bits signed

Longint = Same as Integer
Byte = 8 bits unsigned

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.
String = 'String' is somehow similar to 'ShortString'. Its length can be up to 2GB. Though, rarely used, Delphi also support PChar strings, which are used in our Hardware ID Extractor DLL, to make it compatible with other programming languages.

 

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;

 

 

 

C/C++ Type ObjectPascal Type
unsigned short [int] Word
[signed] short [int] SmallInt
unsigned [int] Cardinal
[signed] int Integer
UINT LongInt { or Cardinal }
WORD Word
DWORD LongInt { or Cardinal }
unsigned long LongInt { or Cardinal }
unsigned long int LongInt { or Cardinal }
[signed] long LongInt
[signed] long int LongInt
char Char
signed char ShortInt
unsigned char Byte
char* PChar
LPSTR or PSTR PChar
LPWSTR or PWSTR PWideChar
void* Pointer
BOOL Bool
float Single
double Double
long double Extended
LP,NP,PP,P prefix: if first = T then T becomes P else P prefix
HANDLE THandle
FARPROC TFarProc
ATOM TAtom
TPOINT TPoint
TRECT TRect
COLORREF TColorRef
OFSTRUCT TOFStruct
DEBUGHOOKINFO TDebugHookInfo
BITMAP TBitMap
RGBTRIPLE TRGBTriple
RGBQUAD TRGBQuad
BITMAPCOREHEADER TBitmapCoreHeader
BITMAPINFOHEADER TBitmapInfoHeader
BITMAPINFO TBitmapInfo
BITMAPCOREINFO TBitmapCoreInfo
BITMAPFILEHEADER TBitmapFileHeader
HANDLETABLE THandleTable
METARECORD TMetaRecord
METAHEADER TMetaHeader
METAFILEPICT TMetaFilePict
TEXTMETRIC TTextMetric
NEWTEXTMETRIC TNewTextMetric
LOGBRUSH TLogBrush
LOGPEN TLogPen
PATTERN TPattern { TLogBrush }
PALETTEENTRY TPaletteEntry
LOGPALETTE TLogPalette
LOGFONT TLogFont
ENUMLOGFONT TEnumLogFont
PANOSE TPanose
KERNINGPAIR TKerningPair
OUTLINETEXTMETRIC TOutlineTextMetric
FIXED TFixed
MAT2 TMat2
GLYPHMETRICS TGlyphMetrics
POINTFX TPointFX
TTPOLYCURVE TTTPolyCurve
TTPOLYGONHEADER TPolygonHeader
ABC TABC
RASTERIZER_STATUS TRasterizer_Status
MOUSEHOOKSTRUCT TMouseHookStruct
CBTACTIVATESTRUCT TCBTActivateStruct
HARDWAREHOOKSTRUCT THardwareHookStruct
EVENTMSG TEventMsg
WNDCLASS TWndClass
MSG TMsg
MINMAXINFO TMinMaxInfo
SEGINFO TSegInfo
ACCEL TAccel
PAINTSTRUCT TPaintStruct
CREATESTRUCT TCreateStruct
CBT_CREATEWND TCBT_CreateWnd
MEASUREITEMSTRUCT TMeasureItemStruct
DRAWITEMSTRUCT TDrawItemStruct
DELETEITEMSTRUCT TDeleteItemStruct
COMPAREITEMSTRUCT TCompareItemStruct
WINDOWPOS TWindowPos
WINDOWPLACEMENT TWindowPlacement
NCCALCSIZE_PARAMS TNCCalcSize_Params
SIZE TSize
MENUITEMTEMPLATEHEADER TMenuItemTemplateHeader
MENUITEMTEMPLATE TMenuItemTemplate
DCB TDCB
COMSTAT TComStat
MDICREATESTRUCT TMDICreateStruct
CLIENTCREATESTRUCT TClientCreateStruct
MULTIKEYHELP TMultiKeyHelp
HELPWININFO THelpWinInfo
CTLSTYLE TCtlStyle
CTLtype TCtltype
CTLINFO TCtlInfo
DDEADVISE TDDEAdvise
DDEDATA TDDEData
DDEPOKE TDDEPoke
DDEAACK TDDEAck
DEVMODE TDevMode
KANJISTRUCT TKanjiStruct

 

More

 

free software
contig assembly software
   
  Hardware ID Extractor