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,2a993df02f6d82f1 X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: More questions... Date: 1999/03/10 Message-ID: #1/1 X-Deja-AN: 453229439 Sender: matt@mheaney.ni.net References: <7bvb4j$lt0$1@remarQ.com> <7bvn7g$rpm@bgtnsc03.worldnet.att.net> <36E43C0D.5D75DB8A@prolifics.com> NNTP-Posting-Date: Tue, 09 Mar 1999 18:51:35 PDT Newsgroups: comp.lang.ada Date: 1999-03-10T00:00:00+00:00 List-Id: Hyman Rosen writes: > "James S. Rogers" writes: > > Ada does not have a direct equivalent to references. In fact, Ada > > access types are as safe as C++ references while also being useful > > in all Ada data structures. > > C++ reference types have little to do with safety, and much to do with > operator overloading. Their main purpose is to permit the return value > of a function to be the alias of an existing object, so that further > operations may be performed on it, and to avoid copying of possibly > large objects as function parameters. The mechanism for this sort of thing in Ada is to have a function that returns a pointer designating the object, and then dereference the result of the function. For example, suppose you have a stack of integers. The bullet-proof way to set the top item via a "reference" (really, a pointer) is to declare the operation function Set_Top (Stack : access Stack_Type) return Item_Access; and then do this: Set_Top (Stack'Access).all := 10; Using this approach, you can never have a dangling reference. It's not as nice as the C++ way: Set_Top (Stack) := 10; but it's reasonably close. In Ada95 you pay a small amount of syntactic overhead, but the benefit is that you can't get a dangling reference. If you know what you're doing, you can shorten the statement to Set_Top (Stack).all := 10; I discuss this technique in my article "Collections of Limited Items" in the ACM patterns archive.