Board index » delphi » Cannot override a static method (TObject.Create override?)

Cannot override a static method (TObject.Create override?)

I'm setting up a very simple object that needs to do its own mem. allocation
and deallocation.  The code is something like this:

TFoo = class(TObject)
private
        ...
public
        constructor Create; override;
        destructor Destroy; override;
end;

I get the "cannot override a static method' error on both the constructor and
destructor.  My guess is that I'm overlooking something simple, but I do have
other code (that deals with TComponent objects) that works just fine - I don't
see what's different here (except that Create has no parameters).

Any hints would be appreciated!

Jan

 

Re:Cannot override a static method (TObject.Create override?)


Jan C. Zawadzki <j...@mgma.com> wrote in article
<58sef2$...@shiva.usa.net>...

Quote
> I'm setting up a very simple object that needs to do its own mem.
allocation
> and deallocation.  The code is something like this:

> TFoo = class(TObject)
> private
>    ...
> public
>    constructor Create; override;
>         destructor Destroy; override;
> end;

> I get the "cannot override a static method' error on both the constructor
and
> destructor.  My guess is that I'm overlooking something simple, but I do
have
> other code (that deals with TComponent objects) that works just fine - I
don't
> see what's different here (except that Create has no parameters).

> Any hints would be appreciated!

Hi Jan,

Well, I don't think overriding Create and Destroy is the right approach.
According to my info, you should override NewInstance and FreeInstance,
which do the actual alloc/free.

Regards,
Jarle stabell

Re:Cannot override a static method (TObject.Create override?)


In article <58sef2$...@shiva.usa.net>, j...@mgma.com (Jan C. Zawadzki) wrote:

Quote
>I'm setting up a very simple object that needs to do its own mem. allocation
>and deallocation.  The code is something like this:

>TFoo = class(TObject)
>private
>        ...
>public
>        constructor Create; override;
>        destructor Destroy; override;
>end;

>I get the "cannot override a static method' error on both the constructor and
>destructor.  My guess is that I'm overlooking something simple, but I do have
>other code (that deals with TComponent objects) that works just fine - I don't
>see what's different here (except that Create has no parameters).

>Any hints would be appreciated!

TObject's definition of Create and Destroy is as a static method. Descendant
classes can do

constructor Create ; Virtual ;

then the classes that descent from it can do

constructor create ; override ;

John

Wake up and smell the Clintons!

Re:Cannot override a static method (TObject.Create override?)


In article <58sef2$...@shiva.usa.net>,
   j...@mgma.com (Jan C. Zawadzki) wrote:
]-I'm setting up a very simple object that needs to do its own mem. allocation
]-and deallocation.  The code is something like this:
]-
]-TFoo = class(TObject)
]-private
]-      ...
]-public
]-      constructor Create; override;
]-        destructor Destroy; override;
]-end;
]-
]-I get the "cannot override a static method' error on both the constructor and
]-destructor.  My guess is that I'm overlooking something simple, but I do have
]-other code (that deals with TComponent objects) that works just fine - I don't
]-see what's different here (except that Create has no parameters).
]-
]-Any hints would be appreciated!

use the "virtual" directive in your base class; e.g. for
your TFoo class..

    constructor Create; virtual;
    destructor Destroy; virtual;

any subsequent classes that inherit from your base class
would then use the "override" directive.

Mark Vaughan

]-
]-Jan
]-

Re:Cannot override a static method (TObject.Create override?)


Quote
Mark Vaughan wrote:
> use the "virtual" directive in your base class; e.g. for
> your TFoo class..

>     constructor Create; virtual;
>     destructor Destroy; virtual;

> any subsequent classes that inherit from your base class
> would then use the "override" directive.

The original poster only seemed to be interested in setting up the one
class.  In this case, define Create and Destroy without any 'virtual' or
'override' keywords.  Also, don't use 'inherit' within a method when
you're not overriding it.

/* Aaron */

Re:Cannot override a static method (TObject.Create override?)


Quote
Aaron Wieland wrote:

> Also, don't use 'inherit' within a method when
> you're not overriding it.

Why not?

M.

--
Ettertraktet kaffe, er det ekstra god kaffe?

mailto:martin.lars...@delfi-data.msmail.telemax.no
http://www.delfidata.no/users/~martin

Re:Cannot override a static method (TObject.Create override?)


On Fri, 13 Dec 96 21:50:34 GMT, mi...@worldnet.att.net (John M. Miano)
wrote:

Quote
>TObject's definition of Create and Destroy is as a static method. Descendant
>classes can do

>constructor Create ; Virtual ;

>then the classes that descent from it can do

>constructor create ; override ;

TObject.Create is static, but TObject.Destroy is virtual.

DO NOT declare Destroy as virtual in any class. That hides
TObject.Destroy, and TObject.Free calls TObject.Destroy.
Always use override when declaring Destroy.
--
Ray Lischner, Tempest Software, Inc., Corvallis, Oregon, USA
Author of Secrets of Delphi 2 (http://www.tempest-sw.com/secrets/)

Re:Cannot override a static method (TObject.Create override?)


Quote
Martin Larsson wrote:

> Aaron Wieland wrote:

> > Also, don't use 'inherit' within a method when
> > you're not overriding it.

> Why not?

Oops, how embarassing...  My "rule" about not using 'inherited' for a
static method was completely false.  Mr. Larsson is correct -- there's
no reason why you can't inherit in either case, though I don't think
that there is any point in using 'inherited' with a direct descendant of
TObject.

Thanks for the correction,

Aaron

Re:Cannot override a static method (TObject.Create override?)


Quote
Aaron Wieland wrote:

> Martin Larsson wrote:

> > Aaron Wieland wrote:

> > > Also, don't use 'inherit' within a method when
> > > you're not overriding it.

> > Why not?

> Oops, how embarassing...  My "rule" about not using 'inherited' for a
> static method was completely false.  Mr. Larsson is correct -- there's
> no reason why you can't inherit in either case, though I don't think
> that there is any point in using 'inherited' with a direct descendant of
> TObject.

There's no point _now_. TObject.Create and TObject.Destroy are emtpy.
BUT, that might change. Or you might change the parent of you class.
("Hm, this could actually be a component! Well, I'll just change the
parent class from TObject to TComponent.")
So, just to keep the code easier to maintain, _always_ call inherited
when you override something, unless you explicitly don't want that
implementation. In that case, the reason for not calling inherited
should be documented.

M.

--
Ettertraktet kaffe, er det ekstra god kaffe?

mailto:martin.lars...@delfi-data.msmail.telemax.no
http://www.delfidata.no/users/~martin

Other Threads