* Can a child access private types of its parent?
@ 2009-07-21 11:42 vlc
2009-07-21 12:05 ` Dmitry A. Kazakov
2009-07-21 12:07 ` Martin
0 siblings, 2 replies; 5+ messages in thread
From: vlc @ 2009-07-21 11:42 UTC (permalink / raw)
Hi *,
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;
28
29 begin
30
31 Put (Var.Get);
32
33 end Main;
But the compiler gives me a
main.adb:23:21: no selector "I" for type "C" defined at line 17
The code compiles if I make the type P public, i.e. if I comment the
lines 06 and 07, the code compiles as the variable I gets inherited to
Child. But then everybody can not only read but also write Var.I, e.g.
29 begin
30
31 Var.I := 2;
32 Put (Var.Get);
33
34 end Main;
Is there a way to inherit private types from a Parent to its child?
Thank a lot in advance for any help!
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Can a child access private types of its parent?
2009-07-21 11:42 Can a child access private types of its parent? vlc
@ 2009-07-21 12:05 ` Dmitry A. Kazakov
2009-07-21 12:07 ` Martin
1 sibling, 0 replies; 5+ messages in thread
From: Dmitry A. Kazakov @ 2009-07-21 12:05 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Can a child access private types of its parent?
2009-07-21 11:42 Can a child access private types of its parent? vlc
2009-07-21 12:05 ` Dmitry A. Kazakov
@ 2009-07-21 12:07 ` Martin
2009-07-21 12:16 ` vlc
1 sibling, 1 reply; 5+ messages in thread
From: Martin @ 2009-07-21 12:07 UTC (permalink / raw)
On Jul 21, 12:42 pm, vlc <just.another.spam.acco...@googlemail.com>
wrote:
> Hi *,
>
> 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;
> 28
> 29 begin
> 30
> 31 Put (Var.Get);
> 32
> 33 end Main;
>
> But the compiler gives me a
> main.adb:23:21: no selector "I" for type "C" defined at line 17
>
> The code compiles if I make the type P public, i.e. if I comment the
> lines 06 and 07, the code compiles as the variable I gets inherited to
> Child. But then everybody can not only read but also write Var.I, e.g.
>
> 29 begin
> 30
> 31 Var.I := 2;
> 32 Put (Var.Get);
> 33
> 34 end Main;
>
> Is there a way to inherit private types from a Parent to its child?
>
> Thank a lot in advance for any help!
You need to make the Parent - Child packages (at library level).
e.g.
-- File: parent.ads
package Parent is
-- As you have
end Parent;
-- File: parent-child.ads
package Parent.Child is
-- Rest as you have, or you can miss the "Parent." bit, e.g.
-- type C is new P with private;
...
end Parent.Child;
-- File: parent-child.adb
package body Parent.Child is
-- Rest as you have
end Parent.Child;
-- File: main.adb
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;
Cheers
-- Martin
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2009-07-25 19:57 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-21 11:42 Can a child access private types of its parent? vlc
2009-07-21 12:05 ` Dmitry A. Kazakov
2009-07-21 12:07 ` Martin
2009-07-21 12:16 ` vlc
2009-07-25 19:57 ` Yannick Duchêne Hibou57
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox