Re: Can a child access private types of its parent?



On Tue, 21 Jul 2009 04:42:36 -0700 (PDT), vlc wrote:

in the following code extract, I try to access a private variable I
(line 09) in the Parent's type P from Child (in function Get, line
23):

01 with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
02
03 procedure Main is
04
05 package Parent is
06 type P is tagged private;
07 private
08 type P is tagged record
09 I : integer := 5;
10 end record;
11 end Parent;
12
13 package Child is
14 type C is new Parent.P with private;
15 function Get (This : C) return integer;
16 private
17 type C is new Parent.P with null record;
18 end Child;
19
20 package body Child is
21 function Get (This : C) return integer is
22 begin
23 return This.I;
24 end Get;
25 end Child;
26
27 Var : Child.C;

Is there a way to inherit private types from a Parent to its child?

Child is not a child of Parent. Move Parent out of Main to the library
level and make Child also a library package named Parent.Child. I.e.

package Parent is
type P is tagged private;
private
type P is tagged record
I : integer := 5;
end record;
end Parent;

package Parent.Child is
type C is new Parent.P with private;
function Get (This : C) return integer;
private
type C is new Parent.P with null record;
end Parent.Child;

package body Parent.Child is
function Get (This : C) return integer is
begin
return This.I;
end Get;
end Parent.Child;

with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
with Parent.Child;

procedure Main is
Var : Parent.Child.C;
begin
Put (Var.Get);
end Main;

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
.



Relevant Pages