From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,324453f076c10aeb X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: Exposing agreggate members of a class Date: 1999/01/06 Message-ID: #1/1 X-Deja-AN: 429289301 Sender: matt@mheaney.ni.net References: <76cd39$21c8$1@news.gate.net> NNTP-Posting-Date: Tue, 05 Jan 1999 20:40:06 PDT Newsgroups: comp.lang.ada Date: 1999-01-06T00:00:00+00:00 List-Id: "David Botton" writes: > Given: > > with GCanvas_Package; use GCanvas_Package; > > type GWindow_Object is tagged with private; > type GWindow_Pointer is access all GWindow_Object; > > private: > > type GWindow_Object is tagged > record > Canvas : GCanvas_Object; > end record; > > What is the cleanest way to give access to the Canvas by reference in a way > that will not require Unchecked Programming > > What I don't want: > > function Get_Canvas( window : in GWindow_Object ) return GCanvas_Pointer > is > begin > return window.Canvas'Unchecked_Access; > end Get_Canvas; Then try passing the window object as an access parameter: private type GWindow_Object is tagged record Canvas : aliased GCanvas_Object; end record; function Get_Canvas (Window : access GWindow_Object) return GCanvas_Pointer is begin return Window.Canvas'Access; end; end; Alternatively, you could make Canvas a public attribute of GWindow_Object. It's a more direct way of doing what you were trying to do: package Windows is type GCanvas_Object is new Integer; type GWindow_Object_Public is abstract tagged record Canvas : aliased GCanvas_Object; end record; type GWindow_Object is new GWindow_Object_Public with private; type GWindow_Pointer is access all GWindow_Object; private type GWindow_Object is new GWindow_Object_Public with null record; end Windows; > Which on top of the Unchecked_Access issue means that consumers of such a > class would have to dereference first to use the GCanvas_Object methods: > > declare > Canvas : GCanvas_Pointer := Get_Canvas( window ); > begin > Draw_Line(Canvas.all, 10, 10, 100, 100); > end; > > Of course you could write the methods to accept GCanvas_Pointer's, but I > want to avoid the use of Access variables when possible. Do a deference at the point of invocation of the selector: declare Canvas : GCanvas_Object renames Get_Canvas (Window'Access).all; begin Draw_Line (Canvas, ...); end; (To avoid any unchecked programming, you'll have to pass the window as an access parameter.)