Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback



Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi








Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Converting C++ code to ObjectPascalFormat this article printer-friendly!Bookmark function is only available for registered users!
Pitfalls to be aware of
Product:
Delphi all versions
Category:
Object Pascal
Skill Level:
Scoring:
Last Update:
04/13/2001
Search Keys:
delphi delphi3000 article borland vcl code-snippet c++ objectpascal converting conversion translation
Times Scored:
12
Visits:
6558
Uploader: Frederic Vanmol
Company: Image-Line
Reference: N/A
 
Question/Problem/Abstract:
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 < headername.h >
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





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
static
    pramod bhakta (Aug 27 2002 1:13PM)

Is there any equivalent for static variables of c++ in Delphi? Is there any way to get the instance count of a class in Delhi?
Respond

RE: static
Frederic Vanmol (Aug 27 2002 1:23PM)

My knowledge of static variables isn't so great at the moment. Global variables might do the trick.

I don't know of any built-in way to get the instance count of a class. But you can use a global variable, which you increment in the constructor and decrement in the destructor of your class.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
E. DSpirito
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)