Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2001-10

about create method

4 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ivan Montes

Hi,

It's just a simple question.
Since every class is derived from tObject, when I implement a Constructor in my class I have to call "inherited create;"
or is not necesary?

thks, Ivan

i.e:

myClass = CLASS
constructor create;
destructor destroy;
end;
constructor myClass.create;
begin
   inherited Create;  // Is it necessary?
   //my initialization routine
end;
destructor myClass.destroy;
begin
   //my deinitialization routine
  inherited destroy; // Is it necessary
end;
Michael Van Canneyt
to Ivan Montes
Ivan Montes wrote 7 lines
Hi, It's just a simple question. Since every class is derived from tObject, when I implement a Constructor in my class I have to call "inherited create;" or is not necesary?

For the constructor not really (TObject.Create is not virtual, and empty), but for the destructor it is absolutely necessary. But it is good practice always to call the inherited constructor/destructor.

Michael.

Ingmar Tulva
to Michael Van Canneyt
Michael Van Canneyt wrote 6 lines
> Since every class is derived from tObject, when I implement a Constructor in > my class I have to call "inherited create;" > or is not necesary? For the constructor not really (TObject.Create is not virtual, and empty), but for the destructor it is absolutely necessary.

Are you sure? Isn't TObject.Destroy also empty? All the necessary cleanup is perfomed by the hidden code a the end of _every_ destructor, isn't it (at least this shoud be the way "object" model works)?

But anyway, I agree with your note about good practice.

Ingmar,

Full_Name
to Michael Van Canneyt: For the constructor not really (TObject.Create is not virtual, and empty), but for the…
Show quoted text 6 lines
> It's just a simple question. > Since every class is derived from tObject, when I implement a Constructor in > my class I have to call "inherited create;" > or is not necesary? >

Consider the following scenario:

TBase = class(TObject)
  constructor Create(aParam: integer); virtual;
end;
TParent = class(TBase)
  constructor Create(aParam: integer); override;
end;
TChild = class(TParent)
  constructor Create(aParam: integer); override;
end;

If you do not call 'inherited create(aParam);' in TParent, you will break the inheritence chain. TChild will never be able to be fully initialized as per TBase. If you wish to have this, then do it, but I would advize against it.

For the constructor not really (TObject.Create is not virtual, and empty),

In fact it's a good idea to make your constructor 'virtual' in all classes that inherit from TObject. This saves problems further down the inheritence tree.

but for the destructor it is absolutely necessary.

Vital. Your application may well acess violate or do nasty things when exiting if the inherited destroy is not called.

But it is good practice always to call the inherited constructor/destructor.

You can write conditional code that only calls the inherited version in certain circumstances, and also vary the position of the inherited call in the Constructor (but never the destructor.. if you do, further calls will probably fail due to your object already being freed.)

"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." Kristian Wilson, Nintendo, Inc, 1989 -----BEGIN GEEK CODE BLOCK----- Version: 3.12 GCS d? s+++:+ a- C++ UL+ P L++ E---- W- N+ o+ K- w O- M V PS PE-- Y PGP- t- 5-- X- R- tv+ b+ DI++ D+ G e++ h--- r+++ y+++ ------END GEEK CODE BLOCK------