Re: [fpc-pascal] Constructors & Destructors 101

2009-04-05 Thread Nikolay Nikolov
Jonas Maebe wrote: If you had two different create constructors (for whatever reason), might you not also need two different destroy destructors? No, the default destructor should always free all resources, regardless of how the class instance was created. Otherwise, it would also make your c

Re: [fpc-pascal] Constructors & Destructors 101

2009-04-04 Thread Bart
On 4/3/09, Doug Chamberlin wrote: > Essentially, yes. > > However, you may create subtle, lurking bugs if you omit that call and > later refactor your code. For example, if you later change > > type > TFoo = class > > to > > type > TFoo = class(TSomeClass) > > and TSomeClass has so

Re: [fpc-pascal] Constructors & Destructors 101

2009-04-03 Thread Doug Chamberlin
Bart wrote: On 4/3/09, Jonas Maebe wrote: I think what is meant, is that if you create a direct subclass of TObject, there is no need to call TObject's create constructor (e.g., via "inherited create;") from your own constructors. It doesn't hurt if you do it of course, and may be good practic

Re: [fpc-pascal] Constructors & Destructors 101

2009-04-03 Thread Jonas Maebe
On 03 Apr 2009, at 16:21, Bart wrote: So if i understand correctly: Say I have Type TFoo = class; private fSomeField: Integer; public constructor Create; end; then constructor TFoo.Create begin Inherited Create; fSomeField := -1; end; would in essence be equal to constructor TFoo.Cr

Re: [fpc-pascal] Constructors & Destructors 101

2009-04-03 Thread Bart
On 4/3/09, Jonas Maebe wrote: > I think what is meant, is that if you create a direct subclass of TObject, > there is no need to call TObject's create constructor (e.g., via "inherited > create;") from your own constructors. It doesn't hurt if you do it of > course, and may be good practice to ac

Re: [fpc-pascal] Constructors & Destructors 101

2009-04-03 Thread Flávio Etrusco
> C) Just out of curiosity, am wondering why FreeAndNil is global procedure > instead of a method/destructor of TObject.   I am guessing it is > for compatibility with Delphi which may or may not have a reason? A method could not act the way FreeAndNil works (zeroing a local pointer variable). It

Re: [fpc-pascal] Constructors & Destructors 101

2009-04-03 Thread Jonas Maebe
On 03 Apr 2009, at 03:43, Richard Ward wrote: A) The documentation says that for the create constructor: (quote} Description: Create creates a new instance of TObject. Currently it does nothing. It is also not virtual, so there is in principle no need to call it directly. {unquote}

Re: [fpc-pascal] Constructors & Destructors 101

2009-04-03 Thread Jonas Maebe
On 03 Apr 2009, at 03:43, Richard Ward wrote: 5) Although I can't remember reading it anywhere, I've noticed from my own demo programs that the addresses of objects are initially set to nil. That is not correct. All global variables (classes/objects or not) are initialized to 0/nil, but

Re: [fpc-pascal] Constructors & Destructors 101

2009-04-03 Thread Jonas Maebe
On 03 Apr 2009, at 03:43, Richard Ward wrote: C) Just out of curiosity, am wondering why FreeAndNil is global procedure instead of a method/destructor of TObject. If you have: var c: tsomeclass; begin c:=c.tsomeclass.create; c.freeandnil end; then freeandnil would get, as first (hidd

Re: [fpc-pascal] Constructors & Destructors 101

2009-04-03 Thread leledumbo
> What is it meant by: "no need to call [create] directly?" How do you > invoke the constructor without calling it? ... and ... Why is create > not virtual and the destroy destructor is? Often in other OOP language, you need to call parent constructor before doing anything else. In OP case,

[fpc-pascal] Constructors & Destructors 101

2009-04-02 Thread Richard Ward
I am a little bit unclear about certain things on class constructors and destructors. From what I read in the language reference guide (chapter 6) and runtime utilities document (chapter 29): 1) All classes descend from TObject; even declaring a new class without using TObject as a qualif