According to Thomas Tan:
When I took a look at objects.pas in the \test\borland_\ directory (a copy below), a few questions raise to mind. [...] Program Objects;(* Bug fixed: objects only worked in units, not in programs. *)Type ParentPtr = ^ParentObj; ChildPtr = ^ChildObj;ParentObj = object Constructor Init; Destructor Fini; Procedure O; c: Char; [...]
To me, too. Either your mailer's text editor or your "FirstClass SMTP/NNTP GateWay" or something else has eaten up all line breaks in the source. Or is this just a joke?
- Does the character c have to be desposed in the destructor?
Of course, not. Only *pointer* variables have to be disposed in the destructor. This is the same with GPC as with BP.
If so, is char (0) a null character?
It is, although this has nothing to do with `Dispose'.
- If there are variables (character, arrays ,strings, etc) in a function in
an object, do they have to be manually deleted?
Only if they are pointers.
If so, how would I erase each one of them?
A modified version of the `objects.pas' test program follows below.
Please note that this is a test program which has the only purpose of testing whether the OOP mechanisms in GPC work correctly. For demo programs you can see section "OOP" in the GPC Texinfo online documentation (`info -f gpc -n OOP'). It's not too detailed, but it documents how GPC's OOP differs from that of BP. Since these differences are rather small, any book about "OOP with BP" can help you with GPC, too.
- Is there such a thing as a pointer to a function?
Yes:
Type ProcPtr = ^Procedure; FuncPtr = ^Function ( x: Real ): Real;
If you omit the `^', you have BP's "procedural variables" which work in GPC, too. (* Here and in my other mails, I am referring to recent beta versions of GPC, not to the hopelessly outdated gpc-2.0 from December 1996. ;*)
Hope this helps,
Peter
Dipl.-Phys. Peter Gerwinski, Essen, Germany, free physicist and programmer peter.gerwinski@uni-essen.de - http://home.pages.de/~peter.gerwinski/ [971005] maintainer GNU Pascal [971001] - http://home.pages.de/~gnu-pascal/ [971005]
8< --------------------------------------------------------------------------
Program Objects;
Type StringPtr = ^String; ParentPtr = ^ParentObj; ChildPtr = ^ChildObj;
ParentObj = object S: StringPtr; Constructor Init ( Const SomeString: String ); Destructor Fini; Procedure O; c: Char; (* GNU extension: mixing of fields and methods *) Procedure K; virtual; end (* ParentObj *);
ChildObj = object ( ParentObj ) Procedure O; Procedure K; virtual; end (* ChildObj *);
Var MyObject: ParentPtr;
Constructor ParentObj.Init ( Const SomeString: String );
begin (* ParentObj.Init *) New ( S, length ( SomeString ) ); S^:= SomeString; c:= 'K'; end (* ParentObj.Init *);
Destructor ParentObj.Fini;
begin (* ParentObj.Fini *) Dispose ( S ); end (* ParentObj.Fini *);
Procedure ParentObj.O;
begin (* ParentObj.O *) write ( 'O' ); end (* ParentObj.O *);
Procedure ParentObj.K;
begin (* ParentObj.K *) write ( 'y' ); end (* ParentObj.K *);
Procedure ChildObj.O;
begin (* ChildObj.O *) write ( 'x' ); end (* ChildObj.O *);
Procedure ChildObj.K;
begin (* ChildObj.K *) inherited K; writeln ( chr ( 8 ) + c ); end (* ChildObj.K *);
begin MyObject:= New ( ChildPtr, Init ( 'Hello, world!' ) ); with MyObject^ do begin O; K; writeln ( S^ ); end (* with *); Dispose ( MyObject, Fini ); end.
(* The output should be: *) (* OK *) (* Hello, world! *)