comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic.brenta@insalien.org>
Subject: Re: Ada access vs C/C++ pointers and references
Date: Thu, 19 Aug 2004 23:32:02 +0200
Date: 2004-08-19T23:32:41+02:00	[thread overview]
Message-ID: <87fz6irezx.fsf@insalien.org> (raw)
In-Reply-To: b47de02.0408190733.558c97ba@posting.google.com

Keith H Duggar writes:
>> No. This is just syntax. "." would have worked just fine for
>> dereferencing C/C++ pointers. I suspect the reason for using "->" was
>> just to make it more visible that you are using a pointer.
>
> Originally in C "x->" was shorthand for "(*x)."
>
> However, in C++ "->" serves the very useful purpose of
> (via operator overloading) allowing smart pointer types
> and iterator types to have the same semantics of raw
> pointers in addition to allowing the usual member access.
> Thus
>
> iterator->f()       //access f() in object "pointed" to
> smart_pointer->f()  //access f() in object "pointed" to
> iterator.f()        //access f() in iterator object
> smart_pointer.f()   //access f() in smart pointer object

This is the kind of thing that is really very appealing to gurus, but
after trying to use them for a while I turned away from C++.  If one
overloads "new", "delete", "->" and "()", one quickly gets into a
maintenance nightmare.  For example, suppose I have:

smart_pointer* p;

Then you can easily find yourself writing:

p->f();    // [1]
(*p).f();  // [2]
p.f();     // illegal, I think
(*p)->f(); // [3]

And I'm not even sure anymore what each of these mean :)

> Otherwise, if the pointed to type had a function named f and the
> iterator had a function named f which function would interator.f()
> access?
>
> How is this resolved in Ada?

Resoved? You mean, like a problem?  Ada does not have a problem in
this respect, and so does not need to resolve it.

On the left of ".", you have either a record, or an access to a
record.  On the right of ".", you have one of the members of the
record, or "all" to mean the entire record.  Ada does not allow you to
override the meaning of ".", so you always know exactly what you're
doing.

procedure Proc is
   type Object is ...;
   type Object_Access is access all Object;

   procedure P (O : in out Object) is separate;

   type Iterator is record
      Pointed_To : Object_Access;
   end record;

   procedure P (I : in out Object) is separate;

   type Iterator_Access is access Iterator;

   It : Iterator_Access is new Iterator (Pointed_To => new Object);
begin
   P (It.all);
   P (It.Pointed_To.all); -- implicit dereference of It
   P (It.all.Pointed_To.all); -- explicit dereference of It
end Proc;

As you can see, none of the calls is ambiguous.   

-- 
Ludovic Brenta.



  reply	other threads:[~2004-08-19 21:32 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-18 22:27 Ada access vs C/C++ pointers and references Keith H Duggar
2004-08-19  0:21 ` Nick Roberts
2004-08-19 11:42   ` Marin David Condic
2004-08-19  0:29 ` Jim Rogers
2004-08-19  5:19 ` Ludovic Brenta
2004-08-19  6:21   ` Martin Dowie
2004-08-19 11:55     ` Marin David Condic
2004-08-19 18:01     ` Ludovic Brenta
2004-08-19  7:19   ` j
2004-08-19  7:42     ` Martin Dowie
2004-08-19 12:03     ` Marin David Condic
2004-08-19  7:59 ` Ole-Hjalmar Kristensen
2004-08-19 15:33   ` Keith H Duggar
2004-08-19 21:32     ` Ludovic Brenta [this message]
2004-08-19 22:18       ` Hyman Rosen
2004-08-20  7:52     ` Ole-Hjalmar Kristensen
2004-08-19  8:11 ` Dmitry A. Kazakov
replies disabled

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