comp.lang.ada
 help / color / mirror / Atom feed
From: Chris Powell <chris.powell@rrds.co.uk>
Subject: Re: Help me to chose between ADA 95 and C++
Date: 1999/12/14
Date: 1999-12-14T10:22:56+00:00	[thread overview]
Message-ID: <38561A6C.5DE3D901@rdel.co.uk> (raw)
In-Reply-To: 3855e3cd_1@news1.prserv.net

Matthew Heaney wrote:
> 
> In article <385112AE.7E2CFA9@rdel.co.uk> , Chris Powell
> <chris.powell@rrds.co.uk>  wrote:
> 
> > I would not recommend Ada 95 for OO development. Okay, it has all the
> > benefits of Ada 83 for type safety, etc, but the syntax of its class
> > programming constructs seems to make the code long winded, obscure and
> > error prone.  I can give examples if anyone is interested/disagrees.
> 
> Please do so, especially any examples that you think are "error prone."
> 
> Perhaps there is just a misunderstanding about how to properly declare a
> type.

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.

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

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. So for completeness, the code above in C++ might be:

void Example::Dispatching_Method()
{
   // Dispatch by default
   Another_Dispatching_Method();

   // Scope explicitly with class name to prevent dispatching
   Example::Another_Dispatching_Method();
} 

void Example::Class_Method( Example* instance )
{
   instance->Another_Dispatching_Method();
}

Note that the Class_Method would have been declared static in the class
spec.


This is one example, perhaps a bit verbose. I can try and think of more
if anyone is interested, or please set me straight!


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. 

Having to explicitly pass the object instance as a parameter (the
implicit 'this' in C++) further separates the logical association
between methods and objects.

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?

Chris.




  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 ` Andreas Winckler
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             ` Richard D Riehle
1999-12-14  0:00               ` Matthew Heaney
1999-12-14  0:00             ` Ray Blaak
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             ` Simon Wright
1999-12-15  0:00               ` Chris Powell
1999-12-15  0:00                 ` Robert A Duff
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         ` Marin D. Condic
1999-12-13  0:00         ` Brian Rogoff
1999-12-14  0:00           ` Chris Powell
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-14  0:00             ` Brian Rogoff
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         ` DuckE
1999-12-14  0:00           ` Matthew Heaney
1999-12-14  0:00         ` Matthew Heaney
1999-12-14  0:00           ` Chris Powell [this message]
1999-12-14  0:00             ` Stephen Leake
1999-12-23  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-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