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,FREEMAIL_FROM, INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,893b3f38072bc3ff X-Google-Attributes: gid103376,public From: johnherro@aol.com (John Herro) Subject: Re: access variable Q: Date: 1996/06/19 Message-ID: <4q8bnm$3eh@newsbf02.news.aol.com>#1/1 X-Deja-AN: 160983722 sender: root@newsbf02.news.aol.com references: <31C43F54.74E62073@jinx.sckans.edu> organization: America Online, Inc. (1-800-827-6364) newsgroups: comp.lang.ada Date: 1996-06-19T00:00:00+00:00 List-Id: David Morton writes: > type int_ptr is access integer; > type foo is record > a, b, c : integer; > end record; > bar : foo; > a_ptr := ?????? -- how do I make this point at bar.a ???? In Ada 83, you can't point at an object that has a name, but only at (nameless) objects created with "new." But in Ada 95 we can do what you want. When declaring the object, give the type as "ALIASED Integer" instead of just Integer. When declaring the access type, use "access ALL" instead of just access. Then set your pointer to 'ACCESS. For example, here's a complete program that displays 316 by making A_Ptr point at Bar.A: with Ada.Text_IO; use Ada.Text_IO; procedure Test is type Int_Ptr is access all Integer; type Foo is record A, B, C : aliased Integer; end record; Bar : Foo; A_Ptr : Int_Ptr := Bar.A'Access; begin Bar.A := 316; Put_Line(Integer'Image(A_Ptr.all)); end Test; There's much more to this topic. For example, because Ada is designed to catch as many errors as possible at compile time, Ada will make sure that the lifetime of the object pointed at is at least as long as that of the access type. This is called an "accessibility check," and it avoids the danger of dangling references which plague many C/C++ programs. If you like to live dangerously, you can deliberately override the accessibility check. Also, it's possible to request read-only access to the object pointed at, and to point at *subprograms*. Thus, dynamic dispatching and callback are both possible. There's more information in my Ada Tutor program, available for download at the Web and FTP sites below my signature. I hope this helps. - John Herro Software Innovations Technology http://members.aol.com/AdaTutor ftp://members.aol.com/AdaTutor