Table of Contents Miscellaneous Fundamental Number Variable Types Language Statements Object-Oriented Features Operators Function Keys and Other IDE Tricks Compiler Quirks Appendix I: How to Create a Console App in Delphi Appendix II: Links Appendix III: Sources
Back to the top
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!
s_t s2;
var s, s2: s_t;
int somefunction(int m) // no ";" { // definition ... return 7; }
void someprocedure(int &m, const int n) { ... }
int riskycode(void) { SomeExcptnClass ec; if (...) // a grievous error throw ec; }
/* Note: uncaught exceptions by default terminate the program unless a default exception handler has been registered. */
// or
try z := riskycode(); except on e : SomeExcptnClass do Writeln('caught an error!'); end; (* execution continues here after try block *) ... end;
function riskycode : integer; var ec : SomeExcptnClass; begin if ... then // a grievous error raise ec; end;
(* Note: Uncaught exceptions are handled by Delphi by default, which pops up a dialog and then continues.
Note: it is illegal to have both an except and a finally statement after a try block, which I find very odd.
try ... except is actually rare in common practice. More common is try ... finally, which is used to insure that cleanup gets done even if an exception gets raised.
If you *need* both an except and a finally block, use nested try loops. See "Mastering Delphi 3" page 244. *)
int myclass::get_m(void) { ... }
// etc.
function myclass.get_m : integer; begin ... end;
/* Note that creation and destruction are handled by the class. */
/* A calling program can only refer to an instantiated object, never a class. */
(* Note you must refer to the CLASS when you create an instance of the OBJECT, or you will surely meet the dreaded General Protection Fault. *)
(* Note: creation and destruction must be done explicitly by the CALLER. *)
/* C++ classes have no automatic common ancestor. */
(* Note that all classes ultimately derive from TObject, whether your base class is declared so or not. *)
(* Object Pascal has no private or protected class derivation. *)
// in the .cpp file someclass::someclass(int a) : baseclass(a) { ... // some more initialization }
someclass::~someclass(); { ... // some more cleanup baseclass::~baseclass(); }
type someclass = class (baseclass); constructor Create(a:integer); override; destructor Destroy; override; ... end;
implementation
constructor someclass.Create(a: integer); begin inherited Create(a); ... // some more initialization end;
destructor someclass.Destroy; begin ... // some more cleanup inherited Destroy; end;
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".
F9 (build if necessary, save if options set, run to next breakpoint)
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
In both Visual C++ and Delphi, remember that Undo is your friend, and hopefully these quirks won't cause you to come to grief.
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
Mastering Delphi 3, 2nd Edition, by Marco Cantù. ISBN 0-7821-2052-0
Copyright © 2001 Robert Locher. All rights reserved.