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: Stephen Leake Subject: Re: Help me to chose between ADA 95 and C++ Date: 1999/12/14 Message-ID: #1/1 X-Deja-AN: 560724628 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> <38561A6C.5DE3D901@rdel.co.uk> Organization: NASA Goddard Space Flight Center -- Greenbelt, Maryland USA Newsgroups: comp.lang.ada Date: 1999-12-14T00:00:00+00:00 List-Id: Chris Powell 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