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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,703c4f68db81387d X-Google-Thread: 109fba,703c4f68db81387d X-Google-Thread: 115aec,703c4f68db81387d X-Google-Thread: f43e6,703c4f68db81387d X-Google-Attributes: gid103376,gid109fba,gid115aec,gidf43e6,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!proxad.net!news.wiretrip.org!border2.nntp.ams.giganews.com!nntp.giganews.com!feeder1.cambrium.nl!feed.tweaknews.nl!skynet.be!newspost001!tjb!not-for-mail Date: Wed, 09 Mar 2005 16:23:46 +0100 From: Adrien Plisson User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax) X-Accept-Language: fr-fr, fr-be, fr, en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada,comp.lang.c++,comp.realtime,comp.software-eng Subject: Re: Teaching new tricks to an old dog (C++ -->Ada) References: <4229bad9$0$1019$afc38c87@news.optusnet.com.au> <1110032222.447846.167060@g14g2000cwa.googlegroups.com> <871xau9nlh.fsf@insalien.org> <3SjWd.103128$Vf.3969241@news000.worldonline.dk> <87r7iu85lf.fsf@insalien.org> <1110052142.832650@athnrd02> <1110284070.410136.205090@o13g2000cwo.googlegroups.com> <395uqaF5rhu2mU1@individual.net> <1110377260.350158.58730@z14g2000cwz.googlegroups.com> In-Reply-To: <1110377260.350158.58730@z14g2000cwz.googlegroups.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <422f1502$0$14972$ba620e4c@news.skynet.be> Organization: -= Belgacom Usenet Service =- NNTP-Posting-Host: 51fd0431.news.skynet.be X-Trace: 1110381826 news.skynet.be 14972 81.242.32.207:4852 X-Complaints-To: usenet-abuse@skynet.be Xref: g2news1.google.com comp.lang.ada:8939 comp.lang.c++:44775 comp.realtime:1130 comp.software-eng:4688 Date: 2005-03-09T16:23:46+01:00 List-Id: Hans Malherbe wrote: > Can you program "const correct"? Eg. if you declare a member function > as const the compiler will help you not mutate the object or call any > functions that do. Also, if you pass a parameter as a const reference, > you will not be able to mutate the object the parameter references. well, this one seems problematic to non-C++ users, and especially to Ada users, so i will explain a bit further: let's have a C++ class: class T { public: void dummy() const; void dummy2(); protected: int field; }; T myObject; myObject.dummy(); // here, myObject.field will not change // because dummy is const myObject.dummy2(); // here, myObject.field may change... in C++, dummy() should not be able to call dummy2() since dummy2() is not const, so it may change the state of the object, which is not allowed because dummy() is const. let's see it the Ada way: we dont use the dotted notation. instead we explicitly pass the oject as a parameter, specifying a mode which tells what we want to do with the object: type T is tagged record Field : Integer; end record; procedure Dummy( This : in T ); procedure Dummy_2( This : in out T ); My_Object : T; Dummy( My_Object ); Dummy2( My_Object ); since the "This" parameter in "Dummy" is declared with mode "in", we cant assign a value to any field of "This": "This" will then be constant. also, "Dummy" cannot call "Dummy_2" passing it its "This" parameter: "Dummy_2" requires to be able to assign to "This", but "This" is constant in "Dummy". so the answer is : yes, we can enforce constness in Ada. we can even express more since we have many parameter passing modes available: - with in mode, we cant assign to the parameter - with out mode, we should only assign a value to the parameter (not rely on its initial value)(this one is missing in C++) - with in out mode, we may read and assign the parameter - with access mode, well, this is a reference passing mode (i find it hard to explain the difference with the other modes, someone else may take up on this one) -- rien