Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

Converting C++ code to ObjectPascal

Title: Converting C++ code to ObjectPascal Question: What do you pay attention to when converting C++ to ObjectPascal ? What are the differences between the two ? Answer: This text attempts to help you do conversions from C++ to Delphi, by pointing out some of the pitfalls. It's not complete yet, but I hope to add more from time to time. Any reactions are welcome. Index ----- 0. Before you start 1. Basic rules 2. Modules : Headers and Source files 3. Datatypes 0. Before you start ------------------- a) buy a good book about C/C++ b) buy a good C++ environment, such as C++Builder. Conversions are a lot easier if you can trace through the C++ code to see what happens exactly. 1. Basic rules -------------- a) C and C++ are case-sensitive : In C and C++, bool and Bool are two different things. If you forget this, you're in trouble. b) datatype *variable is a pointer to that datatype : Example : int *myVariable means that myVariable is a pointer to an integer. c) Assignment is done by = Comparison is done by == This one is easy to overlook. Especially the fact that assignment in C++ is the same character as comparison in Delphi, is difficult. C/C++ Delphi ----- ------ = := == = d) { and } are block statements C/C++ Delphi ----- ------ { begin } end /* (* or { */ *) or } // // e) variables are declared throughout the code, not in a special section : Variables are declared by the datatype followed by the variable name. Example : int i; // an integer variable with the name i float f; // a floating point variable with the name f In ObjectPascal, variables can only be declared in a VAR section. In C/C++, they can be declared anywhere in the code. Example : if (v == 1) { int i = 0; i = i + 1; } Here, the variable i is declared inside the if block. As you can also see in this code, variables can be initialized at the moment they are declared (int i = 0). f) objects are not pointers : In Delphi, if you create an object of type TObject, this variable is a pointer. This is not so in C++. Example : suppose there is a class in C++ called TPerson. Then in the following code, personA is not a pointer, but personB is : TPerson personA; TPerson *personB; g) compiler directives C/C++ Delphi ----- ------ #ifdef x {$IFDEF x} #define x {$DEFINE x} As you can see, compiler directives in C++ are preceded by the # character. 2. Header files / Source files ------------------------------ In C/C++, there are two types of modules: headers and source files. Headers usually contain all sorts of definitions which are relevant outside the module. They are comparable to the interface section in Delphi units. Often you'll see something like this : #ifndef _HEADERNAME_H #define _HEADERNAME_H // header stuff here #endif This is called a guard. Because headers are included into every source file, it is necessary to prevent a header from being included twice. The compiler directives in a guard accomplish that. Headers are included in a source file in of of the two following ways : #include "headername.h" #include For conversion purposes, there's no difference. In both cases the header is used by the module. Headers usually don't contain code, but not always. Header files most of the time have either a .h or a .hpp extension. Source files are kind of like the implementation section. They usually have either a .c or a .cpp extension. The first one signifies a C source file, the latter a C++ source file. This difference is important to some compilers, but not when doing a conversion. 3. Data types ------------- If you have C++Builder, you can find this in the help file by looking for "data types". C++ Delphi --- ------ signed char ShortInt short SmallInt int LongInt / Integer unsigned char Byte unsigned short Word unsigned int Cardinal / LongWord bool Boolean BOOL LongBool wchar_t WideChar float Single double Double long double Extended void * Pointer unsigned char PChar / PAnsiChar