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,e5eb8ca5dcea2827 X-Google-Attributes: gid103376,public From: Ray Blaak Subject: Re: Ada OO Mechanism Date: 1999/05/26 Message-ID: #1/1 X-Deja-AN: 482393329 Sender: blaak@LANGLEY References: <7i05aq$rgl$1@news.orbitworld.net> <7i17gj$1u1k@news2.newsguy.com> <7icgkg$k4q$1@nnrp1.deja.com> <3749E9EC.2842436A@aasaa.ofe.org> <7id2eo$fag@drn.newsguy.com> <3749FF7D.F17CE16A@aasaa.ofe.org> <374AC676.F7AE0772@lmco.com> <7ieuja$5v9@news1.newsguy.com> <7ifd6l$bmf@sjx-ixn1.ix.netcom.com> X-Complaints-To: news@bctel.net X-Trace: news.bctel.net 927741040 209.52.142.33 (Wed, 26 May 1999 10:50:40 PDT) Organization: The Transcend NNTP-Posting-Date: Wed, 26 May 1999 10:50:40 PDT Newsgroups: comp.lang.ada Date: 1999-05-26T00:00:00+00:00 List-Id: Hyman Rosen writes: > ...and follows up with code demonstrating the superiority of Ada > array handling over the C++ version. [...] > Array indexing, however, does not normally fall under the heading > of object-oriented techniques. Well, here is an OO example showing difficulties with the C++ model as compared to Ada. It is about a generic/template vector class with binary operators involving scalars. In Ada, due to its method(obj,parm) notation, things are declared in a straight forward way. In C++, friend procedures are required, which is no suprise. The problem with C++, however, is that friend functions cannot be implicitly instantiated when the vector class is instantiated. Ada: generic type ScalarType is (<>); -- Is this right? My Ada is rusty. package Vector is type T is private; function Create(size : Natural) : T; function "*" (vector : in T; scalar : in ScalarType) : T; function "*" (scalar : in ScalarType; vector : in T) : T; private -- whatever end; declare package IntVector is new Vector(Integer); use type IntVector; package FloatVector is new Vector (Float); use type FloatVector; I : IntVector.T := IntVector.Create(3); F : FloatVector.T := FloatVector.Create(3); begin I := I*2; I := 2*I; F := F*2; F := 2*F; end; C++: template class Vector { public: Vector (int size) { /* whatever */ }; ~Vector() {}; Vector operator* (ScalarType scalar) {return Vector(0); /* whatever */}; friend Vector operator* (ScalarType scalar, Vector & V); }; Vector operator* (int scalar, Vector & V) { return V * scalar; } // later: Vector I(3); I = I * 2; I = 2 * I; Vector F(3); F = F * 2; F = 2 * F; In this example, things won't link because I "forgot" to declare the Vector friend function. Not a big deal, since I can just do it. However, it is tedious that I don't get what I need as soon as Vector is instantiated. I could make a mistake in defining it. If one could have template namespaces, then maybe the class and friend function could be defined together, but then one would have to switch to the instantiated namespace (analogous to the "use type" in the Ada above, I suppose). The point is the C++ is a little more clumsy in this regard. If anyone has a way to do the C++ example cleanly, I would be most interested, since I currently use C++ in my projects. In particular, I was playing with defining the friend function as: template Vector operator*(...) {...} but couldn't get it to work. -- Cheers, The Rhythm is around me, The Rhythm has control. Ray Blaak The Rhythm is inside me, blaak@infomatch.com The Rhythm has my soul.