Board index » delphi » Two simple 'object' questions, Delphi 1.0

Two simple 'object' questions, Delphi 1.0

Hello everybody,

I have two very simple questions concerning basic objects, created in
Delphi 1.0:

- When creating my own object class (derived from TObject), I want to
override the Create constructor and Destroy destructor. Somehow, Delphi
doesn't accept the override keyword behind the constructor, but it does
accept it behind the Destroy. Why is that?

- When using the Free method, it seems the object pointer is not reset
to  nil. How is it then possible to check whether the pointer still is a
valid object or not? And is it possible to override the Free method with
a method that does make the object pointer nil?

Thank you very much for your attention.

Kind regards,

Axel.

 

Re:Two simple 'object' questions, Delphi 1.0


The Construtor of TObject is not virtual so there is no need for override

Procedure FreeObject(Var Obj:Tobject);
Begin
   Obj.Free;
   Obj:=Nil;
End;

Axel Horst <a.ho...@student.utwente.nl> schreef in artikel
<33141426.2...@student.utwente.nl>...

Quote
> Hello everybody,

> I have two very simple questions concerning basic objects, created in
> Delphi 1.0:

> - When creating my own object class (derived from TObject), I want to
> override the Create constructor and Destroy destructor. Somehow, Delphi
> doesn't accept the override keyword behind the constructor, but it does
> accept it behind the Destroy. Why is that?

> - When using the Free method, it seems the object pointer is not reset
> to  nil. How is it then possible to check whether the pointer still is a
> valid object or not? And is it possible to override the Free method with
> a method that does make the object pointer nil?

> Thank you very much for your attention.

> Kind regards,

> Axel.

Re:Two simple 'object' questions, Delphi 1.0


Quote
Axel Horst wrote:

> Hello everybody,

> I have two very simple questions concerning basic objects, created in
> Delphi 1.0:

> - When creating my own object class (derived from TObject), I want to
> override the Create constructor and Destroy destructor. Somehow, Delphi
> doesn't accept the override keyword behind the constructor, but it does
> accept it behind the Destroy. Why is that?

Create is not virtual, and therefore cannot be overridden.
A constructor implicitly allocates memory for your object, and when
descending from TObject there are no reason to call inherited Create in
your constructor.

Quote

> - When using the Free method, it seems the object pointer is not reset
> to  nil. How is it then possible to check whether the pointer still is a
> valid object or not? And is it possible to override the Free method with
> a method that does make the object pointer nil?

  Nope. Consider this:

var
  obj1,
  obj2   : TObject;
begin
  obj1 := TObject.Create;
  obj2 := TObject;

  obj2.Free;
end;

Which pointer should be set to nil, and how could Free know how many
variables you have pointing to the TObject instance.

It's good practice to set all references to a object to nil immediately
after freeing the object instance, but you have to do this yourself.

--
Need a custom component? Late on a project? Could use an util?
DOS Device driver? A VxD? NT drivers or services? Applications of any
kind?
Low rates, fast delivery!

When responding to news postings, please CC a copy to my email address.
Thanks.
Erik Sperling Johansen <e...@info-pro.no>

Re:Two simple 'object' questions, Delphi 1.0


Quote
>- When creating my own object class (derived from TObject), I want to
>override the Create constructor and Destroy destructor. Somehow, Delphi
>doesn't accept the override keyword behind the constructor, but it does
>accept it behind the Destroy. Why is that?

TObject.Create is a static constructor, so there's no need to override
it.  You don't get virtual constructors in place until you get up the
hierarchy to TComponent, which has

constructor TComponent.Create(AOwner: TComponent); virtual;

Consequently, "override" is much preferred for TComponent (or its
descendants) - based classes.

(If you have hints and warnings turned on, it will complain if you
use virtual instead of override for them, too)

  --=- Ritchie Annand

Other Threads