comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen_leake@acm.org>
To: comp.lang.ada@ada-france.org
Subject: Re: implicit cast and inheritance
Date: 01 Oct 2004 20:09:34 -0400
Date: 2004-10-01T20:09:34-04:00	[thread overview]
Message-ID: <mailman.154.1096675788.390.comp.lang.ada@ada-france.org> (raw)
In-Reply-To: <cjkfuc$j5s$03$1@news.t-online.com>

"Rick Santa-Cruz" <rick_santa_cruz75@msn.com> writes:

> Hi,
> 
> I have a small question. See the following source-code:
> package Pack is
>     type Base is tagged
>         record
>             Value: Integer;
>         end record;
>     procedure Base_Proc(B: Base);
> 
>     type Derived is new Base with
>         null record;
> end Pack;
> 
> And now I have the following source for the main-procedure:
> with Pack;
> use Pack;
> 
> procedure main is
>     B: Base;
>     D: Derived;
>     procedure Proc(B: Base) is
>     begin
>         null;
>     end Proc;
> begin
>     Pack.Base_Proc(B);    -- 1
>     Pack.Base_Proc(D);    -- 2
>     Proc(B);   -- 3
>     Proc(D);    -- 4
> end main;
> 
> It is clear to me, that the (4) won't compile, but I can solve it in making 
> a cast or defining Proc with 'Class. My question is now why I can call 
> Base_Proc(D). It is clear to me that the class Derived inherit the 
> Base_Proc-Procedure from its base-class, but what is with the parameter? Why 
> does this work?

That is _exactly_ what "inherit" means: at the point where Derived is
declared, there is implicitly declared:

procedure Base_Proc (B : Derived);

So you can call it.

> I have a suggestion why this works... cause it is in the same
> package... but how does it internly work? 

In this case, there is one compiled version of the body of Base_Proc,
and the call Base_Proc (D) calls it. D is passed by reference, so the
body doesn't know or care whether it is getting a Base type or a
Derived type.

> If it is in the same package, is then always implicitly done a cast,
> or is the procedure Base_Proc in the derived class "Derived"
> automatically defined with a parameter Type "Derived"?

There is a difference between declaring a subprogram and providing an
implementation for it.

Hope this helps,

-- 
-- Stephe




  reply	other threads:[~2004-10-02  0:09 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-01 20:50 implicit cast and inheritance Rick Santa-Cruz
2004-10-02  0:09 ` Stephen Leake [this message]
2004-10-02 16:32   ` Rick Santa-Cruz
2004-10-02 21:35     ` Stephen Leake
2004-10-02 22:05       ` Rick Santa-Cruz
2004-10-03 10:23         ` Marius Amado Alves
2004-10-03 13:27           ` CBFalconer
2004-10-03 12:32         ` Stephen Leake
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox