From: Yukicanis <yukicanis@googlemail.com>
Subject: Re: type access Parent'Class
Date: Mon, 21 Nov 2011 11:44:05 -0800 (PST)
Date: 2011-11-21T11:44:05-08:00 [thread overview]
Message-ID: <36568e75-43cb-4767-91de-cc8eb1db0abe@o17g2000yqa.googlegroups.com> (raw)
In-Reply-To: wccr511p8xp.fsf@shell01.TheWorld.com
On Nov 21, 8:33 pm, Robert A Duff <bobd...@shell01.TheWorld.com>
wrote:
> Yukicanis <yukica...@googlemail.com> writes:
> > Dear Group,
>
> > I'm new to Ada and currently playing around with its OOP features.
> > Now, I have the following source:
>
> > procedure Test is
> > type Parent is tagged limited null record;
> > type Parent_Access is access Parent'Class;
> > A : access Parent'Class;
> > B : Parent_Access;
> > procedure Dyn_Disp(X : access Parent'Class) is
> > begin
> > A := X;
> > B := X;
> > end Dyn_Disp;
> > begin
> > null;
> > end Test;
>
> > When I try to complie that with GNAT 4.6.1 I get the following error
> > message:
>
> > test.adb:9:10: expected type "Parent_Access" defined at line 3
> > test.adb:9:10: found type access to "Parent'Class" defined at line 6
>
> > which I don't really understand since type "Parent_Access" is type
> > access to Parent'Class, isn't it?
>
> No, there are three different access types -- Parent_Access,
> the anonymous type of A, and the anonymous type of X. They're all
> "access to Parent'Class", but they are distinct.
>
> There are some implicit conversion rules that make "A := X;" legal.
> But there is no implicit conversion to Parent_Access, so "B := X;"
> is illegal.
>
> You normally want access-to-class-wide types (and many other access
> types) to be "general", which is specified by adding "all":
>
> type Parent_Access is access all Parent'Class;
>
> If you do that, then you could say "B := Parent_Access(X);"
> or "B := A.all'Access;".
>
> But I suggest you avoid anonymous access types, except in certain
> special cases. They are confusing.
>
> I also suggest you put most of your code in packages, rather than
> in the main procedure. That will also help avoid confusion, because
> things behave differently when inside procedures. For example,
> you can't have any dispatching procedures unless you put the type
> in a package spec.
>
> - Bob
Thanks for your replay. I know that things behave differnetly in
packages. I just wanted the minimal example to be as short es possible
to avoid spamming.
next prev parent reply other threads:[~2011-11-21 19:46 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-11-21 19:03 type access Parent'Class Yukicanis
2011-11-21 19:25 ` Adam Beneschan
2011-11-21 19:40 ` Yukicanis
2011-11-21 19:45 ` Robert A Duff
2011-11-21 19:46 ` Yukicanis
2011-11-21 23:45 ` Gene
2011-11-22 8:42 ` Dmitry A. Kazakov
2011-11-22 10:07 ` Georg Bauhaus
2011-11-22 13:27 ` Simon Wright
2011-11-22 16:13 ` Georg Bauhaus
2011-11-21 19:33 ` Robert A Duff
2011-11-21 19:44 ` Yukicanis [this message]
2011-11-24 10:33 ` Yannick Duchêne (Hibou57)
2011-11-24 11:18 ` Yukicanis
2011-11-21 21:09 ` Jeffrey Carter
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox