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,6b6619eb9cada212 X-Google-Attributes: gid103376,public From: Chris Powell Subject: Re: Help me to chose between ADA 95 and C++ Date: 1999/12/14 Message-ID: <38561A6C.5DE3D901@rdel.co.uk>#1/1 X-Deja-AN: 560477013 Content-Transfer-Encoding: 7bit References: <01bf37fb$a91afb60$0564a8c0@IS-D2D04C.test> <829rbv$a8m$1@nntp6.atl.mindspring.net> <01bf3e32$0b9dc880$022a6282@dieppe> <385112AE.7E2CFA9@rdel.co.uk> <3855e3cd_1@news1.prserv.net> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-Complaints-To: postmaster@rdel.co.uk X-Trace: rdel.co.uk 945166976 817 172.16.10.14 (14 Dec 1999 10:22:56 GMT) Organization: Blackbox Software UK Ltd Mime-Version: 1.0 NNTP-Posting-Date: 14 Dec 1999 10:22:56 GMT Newsgroups: comp.lang.ada Date: 1999-12-14T10:22:56+00:00 List-Id: Matthew Heaney wrote: > > In article <385112AE.7E2CFA9@rdel.co.uk> , Chris Powell > 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.