comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: More questions...
Date: 1999/03/10
Date: 1999-03-10T00:00:00+00:00	[thread overview]
Message-ID: <m34snuvyt3.fsf@mheaney.ni.net> (raw)
In-Reply-To: 36E43C0D.5D75DB8A@prolifics.com

Hyman Rosen <hymie@prolifics.com> writes:

> "James S. Rogers" <jimmaureenrogers@worldnet.att.net> 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.

<http://www.acm.org/archives/patterns.html>














  




  reply	other threads:[~1999-03-10  0:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-03-07  0:00 More questions Michael Young
1999-03-08  0:00 ` James S. Rogers
1999-03-08  0:00   ` Hyman Rosen
1999-03-10  0:00     ` Matthew Heaney [this message]
1999-03-08  0:00 ` Steve Doiel
1999-03-09  0:00 ` Samuel Mize
1999-03-09  0:00   ` Hyman Rosen
1999-03-10  0:00   ` robert_dewar
1999-03-09  0:00 ` Samuel Mize
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox