Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-08

Access inherited^2

8 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Johann Glaser

Hi!

How can I access an inherited inherited method which was overloaded?

====== Example: ======

{$mode objfpc}{$H+}
Program TestInherited;
Uses Classes, SysUtils;
Type
  TFirst = class
    Constructor Create(A,B,C:Integer);
  End;
  TSecond = class(TFirst)
    Constructor Create(A,B:Integer);
  End;
  TThird = class(TSecond)
    Constructor Create(A,B:Integer);
  End;
Constructor TFirst.Create(A, B, C: Integer);
Begin
  { ... }
End;
Constructor TSecond.Create(A, B: Integer);
Begin
  inherited Create(A,B,0);
End;
Constructor TThird.Create(A, B: Integer);
Begin
  inherited Create(A,B,0);   { <-- Error: Wrong number of parameters specified for call to "Create" }
End;
Begin
End.

===================================

The compiler complains in TThird.Create that the call to the inherited method doesn't work. Removing the word "inherited" doesn't help.

What happened to the method with 3 parameters? Why is there an error message? How can I access it?

Thanks
Hansi

Reply by email

Alexey Taran
to Johann Glaser
Johann Glaser wrote
Hi! How can I access an inherited inherited method which was overloaded?

AFAIK TThird inherits Create method not from TFirst, but from TSecond, i.e. TThird.Create calls TSecond.Create, not TFirst.Create.

Reply by email

Johann Glaser
to Joao Morais: You can't, one of them will need to have a different name. Joao Morais

Hi!

Joao Morais wrote
Johann Glaser wrote: > How can I access an inherited inherited method which was overloaded? You can't, one of them will need to have a different name.

Thanks.

Why is a method hidden, just because it has the same name? Isn't it a totally different method, because it has different parameters? Then it should be visible. Or did I miss something?

Bye
Hansi

Reply by email

Joao Morais
to Johann Glaser
Johann Glaser wrote 12 lines
Hi! Am Montag, den 04.08.2008, 08:23 -0300 schrieb Joao Morais: > Johann Glaser wrote: >> How can I access an inherited inherited method which was overloaded? > You can't, one of them will need to have a different name. Thanks. Why is a method hidden, just because it has the same name? Isn't it a totally different method, because it has different parameters? Then it should be visible. Or did I miss something?

Well, afaik you cannot overload methods which belongs to different classes, even if one inherits from the other. Otoh, note that the Zaher's tip should work very well.

Imho a better approach is change the constructor name if you _really_ need to use two incompatible params list. Even better would be creating constructors with the same syntax. You can use default parameters to accomplish this.

Joao Morais

Reply by email

Vincent Snijders
to Johann Glaser: Hi! How can I access an inherited inherited method which was overloaded? ====== Example:…
Johann Glaser wrote
Hi! How can I access an inherited inherited method which was overloaded?

Try the following, mark the constructors as overloaded:

Show quoted text 9 lines
====== Example: ====== {$mode objfpc}{$H+} Program TestInherited; Uses Classes, SysUtils; Type TFirst = class
     Constructor Create(A,B,C:Integer); overload;
End; TSecond = class(TFirst)
     Constructor Create(A,B:Integer); overload;
End; TThird = class(TSecond)
      Constructor Create(A,B:Integer); overload;
Show quoted text 28 lines
End; Constructor TFirst.Create(A, B, C: Integer); Begin { ... } End; Constructor TSecond.Create(A, B: Integer); Begin inherited Create(A,B,0); End; Constructor TThird.Create(A, B: Integer); Begin inherited Create(A,B,0); { <-- Error: Wrong number of parameters specified for call to "Create" } End; Begin End. =================================== The compiler complains in TThird.Create that the call to the inherited method doesn't work. Removing the word "inherited" doesn't help. What happened to the method with 3 parameters? Why is there an error message? How can I access it?

The constructor with 3 parameters is hidden.

Vincent

Reply by email

Johann Glaser
to Vincent Snijders

Hi!

Vincent Snijders wrote 23 lines
Johann Glaser schreef: > Hi! > > How can I access an inherited inherited method which was overloaded? Try the following, mark the constructors as overloaded: > > ====== Example: ====== > > {$mode objfpc}{$H+} > Program TestInherited; > Uses Classes, SysUtils; > > Type > TFirst = class Constructor Create(A,B,C:Integer); overload; > End; > TSecond = class(TFirst) Constructor Create(A,B:Integer); overload; > End; > TThird = class(TSecond) Constructor Create(A,B:Integer); overload; > End;

Thanks, that really helped now. While Zaher Dirkey's hint (Mon, 4 Aug 2008 15:24:03 +0300) also helped and did it's job, it had one drawback: If TFirst.Create raised an exception, this could not by caught by TThird.Create. Even worse, it "converted" to an "Access violation" exception.

I debugged this issue and found that it happend at a "call 0x30(%ecx)" instruction in TThird.Create. ECX pointed to a memory structure where address 0x30 was $00000000. This seems very likely to be a problem with internals of FPC's handling of constructors and exceptions.

Using "overload" now works like a charm. Michael, could you please add this to the documentation?

Thanks
Hansi

Reply by email