(Click here to go back to the index page for my white papers.)
This white paper with the academic-sounding title is intended to be a quick reference guide in tabular format for a C++ programmer trying to learn Delphi's Object Pascal. (To be more specific, my background is in Visual C++ 6 and I am learning Delphi 3.) It may also be useful in the other direction. While I do take the time to mention a few common pitfalls, this document is obviously not intended to single-handedly teach one language or the other, to get bogged down in finer points, or to debate pros and cons. If you have anything to add to this document, email me and I will be happy to consider it; this is a work in progress. All copyrights and trademarks still belong to their rightful owners, especially for the icons.
Table of Contents
Fundamental Number Variable Types
Function Keys and Other IDE Tricks
Appendix I: How to Create a Console App in Delphi
Variable types are subject to size changes with new compiler versions or OS versions. (This is why in Visual C++ 6 int and long are identical.) These sizes are for Visual C++ 6 and Delphi 3 on Windows 95/98/ME or Windows NT 4/2000 (Intel versions).
I left the umpteen character and string types out of this table on purpose.
* I wouldn't use char as a number type because it is unclear (and in fact can be changed with the compiler switch /J in Visual C++ 6) whether it is signed or not.
** Real isn't implemented in Intel hardware, and is only there for backward compatibility. Don't use it!
The numbers in the Operator column indicate precedence groups. For example, the C++ operators & and * have the same precedence because they are in the same group, (3). Not all operators are listed -- operators that are the same in both languages (multiply, add, etc.) are generally omitted. I haven't researched Object Pascal enough to know if the operator precedence is the same.
Note that the and and or operators cause problems for new Object Pascal programmers coming from C++. The problem is that and and or are used for both the logical and the bitwise versions of the operators. What this means is that a statement such as "if x > 5 and x < 7 then" will get you into trouble. Why, you ask? The reason is that the bitwise versions of the and and or operators have a high precedence in Object Pascal. In the example, the first thing the compiler will do is attempt to evaluate "5 and x" using the bitwise version of and, which is not what you intended. The solution is to use parentheses every time you use logical ands or ors in an if statement, like so: "if (x > 5) and (x < 7) then".
There is not an exact correspondence between the function keys in Visual C++ 6 and Delphi 3. Because of this, people typically develop different ways of doing similar tasks in the two IDEs.
Task
save / compile / run
compile only
run only (ignore breakpoints)
run only (stop for breakpoints)
object inspector
pull up the form
view all files in a project
pick a particular file
pick a particular form
find a reference to a variable
find a function (or procedure)
view class framework hierarchy, including user-defined derived classes
go to code definition of a keyword
set / clear breakpoint
trace into
trace over
trace out of
run to cursor
Visual C++
Delphi
First, go to Tools -> Environment Options... and click on the Preferences tab. Check "Editor files" under the "Autosave options" group and click Ok.
F9 (build if necessary, save if options set, run to next breakpoint)
Ctrl+F9 (compile)
n/a -- use F9 (build if necessary, save if options set, run to next breakpoint)
use F9 (build if necessary, save if options set, run to next breakpoint)
F11
F7 (build and save), Ctrl+F5 (run ignoring breakpoints) or F5 (run to next breakpoint)
F7 (build and save)
Ctrl+F5 (run)
F5 (go)
n/a
Resources tab of the workspace pane
FileView tab of the workspace pane
FileView tab of the workspace pane
Resources tab of the workspace pane
source browser (Alt+F12)
ClassView tab of the workspace pane
n/a (use help)
right-click, Go To Definition Of ...
F12
Project Manager
Ctrl+F12
Shift+F12
symbol browser
n/a -- use symbol browser
View->Browser...
Ctrl+(click on keyword) (Delphi 4 or later)
click in the gutter next to the line of code
F9
F11
F7
F10
F8
n/a
Shift+F11
Ctrl+F10
F4
In both Visual C++ (version 6) and Delphi (versions 3 - 5), each compiler has strange quirks to how it processes a file. Since these seem to be poorly documented, I will mention them here.
In Visual C++, if the Project Options say that the file being compiled uses precompiled headers, don't put anything but comments and blank lines before the #include "stdafx.h" directive. Any such lines will be ignored. Also, in an MFC application, be careful about modifying any of the weird comment lines put there by the Class Wizard, or any of the lines in a block of code delimited by the weird comments. If you make any such modifications, you will break the Class Wizard. The only exception I know of is that you may freely change the value to which a variable is initialized inside a block of code delimited by the wizard's comments. I recommend you use the Class Wizard to make changes wherever possible. It will prompt you to manually delete a block of code if necessary, for example if you decide to remove an event handler.
Delphi is kinder to your source code, in that it doesn't mark it up with unsightly comments. However, make no mistake, the compiler makes a clear distinction (which unfortunately is invisible to you in the editor) between lines of code you write and lines of code it writes. It will normally maintain lines of code it inserts; for example, if you save the file, empty function and procedure declarations inserted when you double-clicked something to create an event handler go away. If you modify Delphi's code, on the other hand, Delphi assumes you have taken over maintenance for those lines from now on. This will probably cause you problems. The general rule is, if you don't modify Delphi's code, Delphi won't modify your code. Delphi's code includes function and procedure stubs for event handlers. It also includes all the code from the top of the file, to the last line of the section of the declaration for the form class that isn't declared public or private (etc.). In this example,
...
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
private
everything up to the "private" line is Delphi's code.
In both Visual C++ and Delphi, remember that Undo is your friend, and hopefully these quirks won't cause you to come to grief.
I am of the opinion that console apps are an essential tool of every programmer, even a guru, if for no other purpose than to test your helper classes and such. The creators of Delphi apparently don't much agree with me, because there is no obvious way to create a bare-bones Delphi console app in Delphi 3. (It is pretty simple in Delphi 5.) Still, it can be done.
Create a new project in Delphi.
Go to Project -> Options... , choose the Linker tab, check the "Generate console application" box, and click Ok.
In your Pascal unit, comment out the two lines that say "var" and "Form1: TForm1;". Compile your application with Ctrl-F9 or just F9. Delphi will report an error and open your project file as a text document. (Alternately, to view the source code version of your project file you could have selected View -> Project Source.) The offending highlighted line of code will say "Application.CreateForm(TForm1, Form1);". Comment out this line. (If you don't comment it out, when you run the application you will have a blank form pop up after your console closes.)
You might think that you could clean up your code by deleting the TForm1 class declaration in your unit. Unfortunately, that doesn't work because Delphi is strict about requiring at least one form, even if an instance of the form is never created and the form is never used. As far as I can tell, you still need the "uses" declaration line, though it might be possible to cut out some of the units listed.
You should now be able to compile and run your application. Here is some code to get you started:
begin
Writeln('Hello World!');
Writeln('Press <enter> to exit');
Readln;
end.
I found an excellent page that sets out to do the same thing this page does, but not in quite such a tabular format. It does a much better job of discussing the differences, and is wonderfully unbiased. Click here
Here's an article which is extremely biased towards Delphi (check out the background image) and dead wrong about much about Visual C++. I only include the link because it does make a few valid points (along with many invalid ones), and out of a sense of fairness. Click here: you've been warned
C++ Primer Plus, 2nd Edition, by Stephen Prata. ISBN 1-878739-74-3
Mastering Delphi 3, 2nd Edition, by Marco Cantù. ISBN 0-7821-2052-0
Copyright © 2001 Robert Locher. All rights reserved.