Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Virtual methods not working

1 view
Skip to first unread message

Dave Rudolf

unread,
Jul 29, 2000, 3:00:00 AM7/29/00
to
I'm having problems getting virtual methods (or vice versa). It seems that
if I have a descendant class that redefines a method from it's parent type,
whenever I call that method, it executes the old methd and not the redefined
one. I wrote a quick piece of code to illustrate:


-----------------------------------------------------
type TSomeClass = class
function SomeRoutine:STRING; virtual;
end;
type TSomeDescendant = class (TSomeClass)
function SomeRoutine:STRING; virtual;
end;

function TSomeClass.someRoutine:STRING;
begin
someRoutine:='class';
end;


function TSomeDescendant.someRoutine:STRING;
begin
someRoutine:='descnedant';
end;

----------------------------------------------------

Now somewhere else I will have something like:

----------------------------------------------------
var sc:TSomeClass;
begin
sc:=TSomeClass.create;
MessageDlg( sc.SomeRoutine , mtInformation, [mbOk], 0);
sc.destroy;

sc:=TSomeDescendant.create;
MessageDlg( sc.SomeRoutine , mtInformation, [mbOk], 0);
sc.destroy;
end;

----------------------------------------------------

Both calls to SomeRoutine return the parent class's string ("class" ). This
kind of code works fine in BP7, so what is it about Delphi that prevents the
redefined routine from being called?


-------------------------------------------------------
http://members.home.net/dave.t.rudolf
http://members.home.net/stonemason

Damion Moyer-Sims

unread,
Jul 29, 2000, 3:00:00 AM7/29/00
to
"Dave Rudolf" <dave.t...@home.com> wrote in message
news:8lvdnj$7o...@bornews.borland.com...

> I'm having problems getting virtual methods (or vice versa).

Dave,

In order to override the base class' routine, you must use the override
directive. So, your derived class' declaration will look something like
this:

type TSomeDescendant = class (TSomeClass)

function SomeRoutine:STRING; override;
end;

I believe that should take care of it.

Damion


Dave Rudolf

unread,
Jul 29, 2000, 3:00:00 AM7/29/00
to
That'll do it. Much thanx.


-------------------------------------------------------
http://members.home.net/dave.t.rudolf
http://members.home.net/stonemason


"Damion Moyer-Sims" <dam...@teleport.com> wrote in message
news:39833fd9_2@dnews...

0 new messages