Board index » delphi » newbie... override does'nt!

newbie... override does'nt!

I have a component which has several methods including a constructor
which I declare as init.  this component has a child class which
successfuly inherits ALL attributes (not all shown here) but declares a
new constructor init which needs to be different.

A third component inherits from the second and for this own I wish to
override the plot and behave methods.

The whole thing works BUT the methods used are those of the parent, in
fact if I put a breakpoint in the code of the child, I get run time
message (breakpoint is set on line that contains no code)

This used to be simple in DOS Pascal, what am I doing wrong!?

Any ideas ( I know you have, just bare with me and help ;)

Cheers, Jim

TYPE
  TBug = class(TComponent)
  private
  protected
  public
    tx,ty  : Integer;      {position now  (This)}
    nx,ny  : Integer;      {Position next (Next)}
    constructor init(AOwner : TComponent); virtual;
    procedure   Behave; Virtual;
    procedure   Plot;   Virtual;
    function    Home(tx,ix,ty,iy : Integer) : Boolean;
  published
  END;

  TAnt   = Class(TBug)
    constructor init(VAR x,y : Integer); Virtual;
    procedure   Behave; override;
    procedure   plot;   override;

.............

Jim Barr        Machine Conversation
                http://www.wandana.demon.co.uk/
                Best is the enemy of good enough
                Leaves rustle,   blades turn,    water moves

Barrs Law of Recursive futility:---
        If you are smart enough to use one of these.....
                       .....you can probably manage without one!

 

Re:newbie... override does'nt!


Quote
Jim Barr <jimb...@wandana.demon.co.uk> writes:

> I have a component which has several methods including a constructor
> which I declare as init.  this component has a child class which
> successfuly inherits ALL attributes (not all shown here) but declares a
> new constructor init which needs to be different.

> A third component inherits from the second and for this own I wish to
> override the plot and behave methods.

> The whole thing works BUT the methods used are those of the parent, in
> fact if I put a breakpoint in the code of the child, I get run time
> message (breakpoint is set on line that contains no code)

> This used to be simple in DOS Pascal, what am I doing wrong!?

> Any ideas ( I know you have, just bare with me and help ;)

A couple of ideas - I hope you can still read them if I sprinkle them
in among your code.

Quote

> TYPE
>   TBug = class(TComponent)
>   private
>   protected
>   public
>     tx,ty  : Integer;      {position now  (This)}
>     nx,ny  : Integer;      {Position next (Next)}
>     constructor init(AOwner : TComponent); virtual;

                  ^^^^                       ^^^^^^^
This in itself isn't a problem, but I believe that the TComponent
constructor, Create, is virtual, so you could have written:
CONSTRUCTOR Create (AOwner:  TComponent); OVERRIDE;
You do, of course, call INHERITED Create in this constructor, don't you?

Quote
>     procedure   Behave; Virtual;
>     procedure   Plot;   Virtual;
>     function    Home(tx,ix,ty,iy : Integer) : Boolean;
>   published
>   END;

>   TAnt   = Class(TBug)
>     constructor init(VAR x,y : Integer); Virtual;

                                           ^^^^^^^^
Whether you call your constructor Init or Create, I'm almost certain
that you need to use OVERRIDE here, and not VIRTUAL.  Again, you'd
call INHERITED Create or INHERITED Init, whichever you chose to
implement, at the start of the constructor.

Quote
>     procedure   Behave; override;
>     procedure   plot;   override;

> .............

The only other thing I can think that you might be doing wrong (some
people have done it - don't be offended if you haven't) is to call the
wrong constructor.

if you have
VAR MyAnt:  TAnt;
    MyBug:  TBug;

and assuming that you'd renamed your constructors to Create,

You'd have to say

MyAnt := TAnt.Create (Owner);
MyBug := TBug.Create (Owner);

or

MyBug := TAnt.Create (Owner);

depending on what you want.

If I've missed the point, let me know and I'll try again!

--
-----------------------------------
Matt Francomb, Setanta Software Ltd
http://www.demon.co.uk/setanta
-----------------------------------

Re:newbie... override does'nt!


On Tue, 18 Mar 1997 21:24:52 +0000, Jim Barr <jimb...@wandana.demon.co.uk>
wrote:

Quote
>I have a component which has several methods including a constructor
>which I declare as init.  this component has a child class which
>successfuly inherits ALL attributes (not all shown here) but declares a
>new constructor init which needs to be different.

>A third component inherits from the second and for this own I wish to
>override the plot and behave methods.

>The whole thing works BUT the methods used are those of the parent, in
>fact if I put a breakpoint in the code of the child, I get run time
>message (breakpoint is set on line that contains no code)

>This used to be simple in DOS Pascal, what am I doing wrong!?

>Any ideas ( I know you have, just bare with me and help ;)

>Cheers, Jim

>TYPE
>  TBug = class(TComponent)
>  private
>  protected
>  public
>    tx,ty  : Integer;      {position now  (This)}
>    nx,ny  : Integer;      {Position next (Next)}
>    constructor init(AOwner : TComponent); virtual;
>    procedure   Behave; Virtual;
>    procedure   Plot;   Virtual;
>    function    Home(tx,ix,ty,iy : Integer) : Boolean;
>  published
>  END;

>  TAnt   = Class(TBug)
>    constructor init(VAR x,y : Integer); Virtual;
>    procedure   Behave; override;
>    procedure   plot;   override;

>.............

>Jim Barr        Machine Conversation
>                http://www.wandana.demon.co.uk/
>                Best is the enemy of good enough
>                Leaves rustle,   blades turn,    water moves

>Barrs Law of Recursive futility:---
>        If you are smart enough to use one of these.....
>                       .....you can probably manage without one!

You may want to change the name of your constructor to the more standard name
'Create'...

Other Threads