comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <Stephen.Leake@gsfc.nasa.gov>
Subject: Re: Help me to chose between ADA 95 and C++
Date: 1999/12/14
Date: 1999-12-14T00:00:00+00:00	[thread overview]
Message-ID: <uu2llb17k.fsf@gsfc.nasa.gov> (raw)
In-Reply-To: 38561A6C.5DE3D901@rdel.co.uk

Chris Powell <chris.powell@rrds.co.uk> writes:

> Okay, this is where I find out that I have been getting it wrong for the
> last year! But here goes:
> 
> package body Example is
> 
>    procedure Dispatching_Method (This : access Object) is
>    begin
>       -- This call correctly redispatches to Another_Dispatching_Method
>       Another_Dispatching_Method (This => Object'Class
> (This.all)'Access);
> 
>       -- This (probably incorrectly) calls this object's
>       -- Another_dispatching_Method directly, without dispatching
>       Another_Dispatching_Method (This => This);
> 
>    end Dispatching_Method;
> 
> 
>    procedure Class_Method (This : access Object'Class) is
>    begin
> 
>       -- This call correctly dispatches, since this is already 
>       -- classwide in this case
>       Another_Dispatching_Method (This => This);
> 
>    end Class_Method;
> 
> 
>    procedure Another_Dispatching_Method (This : access Object) is
>    begin
>       null;
>    end Another_Dispatching_Method;
> 
> end Example;
> 
> It has proven to be a common mistake to neglect to convert an object to
> its class wide type before attempting a redispatch.

Hmm. I don't see why Dispatching_Method should _want_ to re-dispatch.
You could simply override Dispatching_Method, in addition to
Another_Dispatching_Method. Can you give a good example of when this
is not a good idea?

> Passing 'This' as an access parameter is common (since references to
> objects are often stored by an object, but further complicates the
> syntax.

No problem here. The syntax is complicated (I assume you are refering
to "Object'Class (This.all)'Access") because you are doing things the
hard way; just override Dispatching_Method.

> By contrast, C++ provides redispatching as the default behaviour which
> makes more sense to me. If a function is declared virtual (ie
> dispatches) you would normally want dispatching to occur and then have
> to use special syntax to prevent dispatching. In Ada, it is the other
> way round.

In Ada, you have complete control over whether a call dispatches or
not. If the actual parameter is class-wide, you get dispatching.
Otherwise, you get static binding. Static binding is a Good Thing in
some situations; I'm not clear you ever get it in C++.

> Other Ada quibbles:
> 
> C++ encapsulates methods and data in a single structure. In Ada, class
> methods are defined outside the object type so do not appear to 'belong'
> to the type. 

That's what packages are for. If you choose, you can use a programming
style where all subprograms in a package are primitive operations on
a type. Or, you can choose a different style. Ada gives you more
freedom to choose your programming idiom; C++ gives you one idiom.
 
> Having to explicitly pass the object instance as a parameter (the
> implicit 'this' in C++) further separates the logical association
> between methods and objects.

C++ uses "object.method". Ada uses Method (object). Same logical
association. Hmm, if you have more parameters, and don't put the
object first in Ada, you might lose something. Once again, Ada gives
you a choice, C++ doesn't.
 
> I am a Multiple Inheritance fan, because I think I know how to use it
> properly. I do not believe it (always) indicates a bad design, as some
> do. Ada 95 has some constructs to allow different types of MI, but not
> all provided by the more general approach of C++, and again, the syntax
> is obscure involving tagged types within generics, access discrimiated
> records, etc. In C++ you simply define an X as a Y and a Z. In Ada, the
> syntax would have been (if allowed):
> 
>    type Object is new X.Object and new Y.Object with record .... 
> 
> Comments?

I'll have to leave this one for Tuck; I don't really understand what
the problems are with the C++ style of multiple inheritance. But
again, Ada gives you a choice :).

-- Stephe




  parent reply	other threads:[~1999-12-14  0:00 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-11-26  0:00 Help me to chose between ADA 95 and C++ Robert
1999-11-26  0:00 ` Harald Schmidt
1999-11-26  0:00   ` Andreas Winckler
1999-11-26  0:00     ` Florian Weimer
1999-12-04  0:00   ` Richard D Riehle
     [not found]     ` <01bf3e32$0b9dc880$022a6282@dieppe>
1999-12-10  0:00       ` Chris Powell
1999-12-13  0:00         ` Richard D Riehle
1999-12-14  0:00           ` Chris Powell
1999-12-14  0:00             ` Simon Wright
1999-12-15  0:00               ` Chris Powell
1999-12-15  0:00                 ` Robert A Duff
1999-12-14  0:00             ` Richard D Riehle
1999-12-14  0:00               ` Matthew Heaney
1999-12-14  0:00             ` Larry Kilgallen
1999-12-15  0:00               ` Robert A Duff
2000-01-12  0:00                 ` Richard Pinkall-Pollei
1999-12-14  0:00             ` Ray Blaak
1999-12-15  0:00             ` Ted Dennison
1999-12-20  0:00               ` Stefan Skoglund
1999-12-16  0:00             ` Pascal Obry
1999-12-16  0:00               ` Aidan Skinner
1999-12-16  0:00               ` Lutz Donnerhacke
1999-12-16  0:00               ` Rakesh Malhotra
1999-12-21  0:00                 ` Geoff Bull
1999-12-21  0:00             ` Robert Dewar
1999-12-21  0:00               ` Chris Powell
1999-12-21  0:00               ` Ted Dennison
1999-12-21  0:00                 ` Robert Dewar
1999-12-13  0:00         ` Brian Rogoff
1999-12-14  0:00           ` Chris Powell
1999-12-14  0:00             ` Brian Rogoff
1999-12-14  0:00             ` Preben Randhol
1999-12-14  0:00               ` Stephen Leake
1999-12-14  0:00                 ` Tucker Taft
1999-12-15  0:00                   ` Stephen Leake
1999-12-15  0:00                 ` Preben Randhol
1999-12-15  0:00           ` Richard Pinkall-Pollei
1999-12-15  0:00             ` Richard Pinkall-Pollei
1999-12-21  0:00             ` Geoff Bull
1999-12-21  0:00               ` Tucker Taft
1999-12-22  0:00                 ` Ted Dennison
1999-12-13  0:00         ` Marin D. Condic
1999-12-13  0:00         ` DuckE
1999-12-14  0:00           ` Matthew Heaney
1999-12-14  0:00         ` Matthew Heaney
1999-12-14  0:00           ` Chris Powell
1999-12-14  0:00             ` Matthew Heaney
1999-12-15  0:00               ` Hyman Rosen
1999-12-14  0:00             ` Tucker Taft
1999-12-14  0:00               ` Matthew Heaney
1999-12-23  0:00               ` Chris Powell
1999-12-27  0:00                 ` Robert A Duff
1999-12-14  0:00             ` Stephen Leake [this message]
1999-12-23  0:00               ` Chris Powell
1999-11-26  0:00 ` Andreas Winckler
1999-11-26  0:00 ` Preben Randhol
1999-11-26  0:00   ` Preben Randhol
1999-11-27  0:00 ` Lionel Draghi
  -- strict thread matches above, loose matches on Subject: below --
1999-12-14  0:00 Robert C. Leif, Ph.D.
1999-12-15  0:00 ` Richard D Riehle
1999-12-15  0:00 Robert C. Leif, Ph.D.
1999-12-16  0:00 ` Richard D Riehle
1999-12-16  0:00   ` Matthew Heaney
1999-12-17  0:00     ` Richard D Riehle
1999-12-18  0:00       ` Matthew Heaney
1999-12-20  0:00         ` Richard D Riehle
1999-12-22  0:00 Help me to chose between ADA 95 and C++ ( Ehud Lamm
replies disabled

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