Access inherited^2

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Access inherited^2

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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Access inherited^2

Alexey Taran
Johann Glaser writes:
> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Access inherited^2

Joao Morais
In reply to this post by Johann Glaser
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.

Joao Morais
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Access inherited^2

zaher dirkey
try (but i not like it )

begin
  TFirst(Self).Create(A, B, 0);
end;
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Access inherited^2

Johann Glaser
In reply to this post by Joao Morais
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?

Bye
  Hansi


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Access inherited^2

Joao Morais
Johann Glaser wrote:

> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Access inherited^2

Vincent Snijders
In reply to this post by Johann Glaser
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;
>
> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Access inherited^2

Johann Glaser
Hi!

Am Sonntag, den 10.08.2008, 10:01 +0200 schrieb Vincent Snijders:

> 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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal