| View previous topic :: View next topic |
| Author |
Message |
Bryan Crotaz Guest
|
Posted: Mon Jul 28, 2003 9:56 am Post subject: Re: [Q] Embaressing inheritance question.... |
|
|
No overloading, even accidentally?
Bryan
"Donovan J. Edye" <donovan (AT) nospam (DOT) edye.wattle.id.au> wrote
| Quote: | G'Day,
If I have the following class hierachy:
TMyBaseClass = class
protected
procedure SomeMethod(const s: string); virtual; abstract;
end;
TMyFirstDesc = class(TMyBaseClass)
protected
procedure SomeMethod(const s: string); override;
end;
TMySecondDesc = class(TMyFirstDescs)
protected
procedure SomeMethod(const s: string); override;
end;
procedure TMyFirstDesc.SomeMethod(const s: string);
begin
inherited;
end;
procedure TMySecondDesc.SomeMethod(const s: string);
begin
inherited; <-- This does not compile
inherited SomeMethod(const s:string); <-- This is required to compile
end;
Can someone please tell me why a simple inherited call in TMySecondDesc
will not work?
TIA
--
-- Donovan J. Edye
----------------------------------------------------------------------
SetiStats - Get your SETI statistics delivered to your mailbox daily.
http://www.edye.wattle.id.au/p.php?page=/delphi/setistats
----------------------------------------------------------------------
|
|
|
| Back to top |
|
 |
Joanna Carter Guest
|
Posted: Mon Jul 28, 2003 12:45 pm Post subject: Re: [Q] Embaressing inheritance question.... |
|
|
Donovan J. Edye wrote:
| Quote: | procedure TMyFirstDesc.SomeMethod(const s: string);
begin
inherited;
end;
|
This will raise an exception if called as it calls an abstract method.
| Quote: | procedure TMySecondDesc.SomeMethod(const s: string);
begin
inherited; <-- This does not compile
inherited SomeMethod(const s:string); <-- This is required to compile
end;
|
Au contraire, the first line compiles and the second one doesn't unless you
change it to
inherited SomeMethod(s);
Is this the real code, or just a sample for demo??
Joanna
--
Joanna Carter
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
|
|
| Back to top |
|
 |
Rob Bracken Guest
|
Posted: Mon Jul 28, 2003 2:55 pm Post subject: Re: [Q] Embaressing inheritance question.... |
|
|
| Quote: | Can someone please tell me why a simple inherited call in TMySecondDesc
will not work? |
Are you sure you've put "override" on the method declaration in
TMySecondDesc? It's easy to forget to do this.
--
Rob Bracken
Bracken Software Ltd.
www.brackensoftware.co.uk
Working with businesses who want to throw their computers out the window
|
|
| Back to top |
|
 |
Donovan J. Edye Guest
|
Posted: Mon Jul 28, 2003 10:07 pm Post subject: Re: [Q] Embaressing inheritance question.... |
|
|
Joanna Carter wrote:
| Quote: | Donovan J. Edye wrote:
| procedure TMyFirstDesc.SomeMethod(const s: string);
| begin
| inherited;
| end;
This will raise an exception if called as it calls an abstract method.
| procedure TMySecondDesc.SomeMethod(const s: string);
| begin
| inherited; <-- This does not compile
| inherited SomeMethod(const s:string); <-- This is required to compile
| end;
Au contraire, the first line compiles and the second one doesn't unless you
change it to
inherited SomeMethod(s);
Is this the real code, or just a sample for demo??
Joanna
| inherited SomeMethod(const s:string); <-- This is required to compile
Yep that is a typo. It should be: |
inherited SomeMethod(s);
| Quote: | Is this the real code, or just a sample for demo??
This is a sample for demo purposes. |
So aside from the typo (*shameful look*) What am I missing here?
--
-- Donovan J. Edye
----------------------------------------------------------------------
SetiStats - Get your SETI statistics delivered to your mailbox daily.
http://www.edye.wattle.id.au/p.php?page=/delphi/setistats
----------------------------------------------------------------------
|
|
| Back to top |
|
 |
Joanna Carter Guest
|
Posted: Tue Jul 29, 2003 8:00 am Post subject: Re: [Q] Embaressing inheritance question.... |
|
|
Donovan J. Edye wrote:
| Quote: | So aside from the typo (*shameful look*) What am I missing here?
|
I copy/pasted your code into D6, commented out the comments, corrected the
typo and it compiled.
Joanna
--
Joanna Carter
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
|
|
| Back to top |
|
 |
Danny Thorpe Guest
|
Posted: Tue Jul 29, 2003 6:28 pm Post subject: Re: [Q] Embaressing inheritance question.... |
|
|
| Quote: | procedure TMySecondDesc.SomeMethod(const s: string);
begin
inherited; <-- This does not compile
inherited SomeMethod(const s:string); <-- This is required to compile
end;
Can someone please tell me why a simple inherited call in TMySecondDesc
will not work? |
The answer depends on which version of Delphi you're using.
Support for "naked" inherited calls is a little spotty in earlier Delphi
compilers. Earlier versions of the compiler did not support naked inherited
calls for methods with parameter lists that were not message methods, and if
the ancestor method was abstract would still generate a call that would fail
at runtime.
Delphi 6 and 7 have improved the handling of naked inherited calls such that
if the ancestor is abstract the compiler will not generate a call at all,
thus saving you the runtime discomfort.
The rule of thumb is: if you use inherited; the compiler is responsible for
selecting an appropriate method if one exists (particularly with respect to
calling an abstract method). If you use inherited methodname(...) then you
are responsible for getting it right - if you specify an abstract method
name, the compiler will gen code to do exactly that regardless of the
runtime consequences.
-Danny
|
|
| Back to top |
|
 |
Danny Thorpe Guest
|
Posted: Tue Jul 29, 2003 11:00 pm Post subject: Re: [Q] Embaressing inheritance question.... |
|
|
"Ritchie Annand" <ritchiea_dontcopythis (AT) malibugroup (DOT) com> wrote
| Quote: | In article <3f26bc6b$1 (AT) newsgroups (DOT) borland.com>, [email]nomail (AT) borland (DOT) com[/email]
says...
Delphi 6 and 7 have improved the handling of naked inherited calls such
that
if the ancestor is abstract the compiler will not generate a call at
all,
thus saving you the runtime discomfort.
!
I didn't even notice that feature sneaking in there. Always used to be
fun inheriting from TThread and Ctrl+Shift+C-ing... and getting caught by
that darned 'inherited' call
|
That was a customer request, actually. I was happy to leave it as it was -
if you tell it to call inherited, fine, we call the inherited even if it's
fatal. But after hearing complaints from several different sources I
thought about it for awhile and decided to change the rule to be more
friendly.
| Quote: |
Now I want to know all the other little tidbits you've been sneaking into
the compiler ;)
|
So do I! ;>
-Danny
|
|
| Back to top |
|
 |
|