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: Hyman Rosen Subject: Re: Ada OO Mechanism Date: 1999/05/26 Message-ID: #1/1 X-Deja-AN: 482428409 Sender: hymie@calumny.jyacc.com 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: abuse@panix.com X-Trace: news.panix.com 927746751 12465 209.49.126.226 (26 May 1999 19:25:51 GMT) Organization: PANIX Public Access Internet and UNIX, NYC NNTP-Posting-Date: 26 May 1999 19:25:51 GMT Newsgroups: comp.lang.ada Date: 1999-05-26T19:25:51+00:00 List-Id: Ray Blaak writes: > Well, here is an OO example showing difficulties with the C++ model > as compared to Ada. > > 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; OK, here's the C++, which compiles without error with EGCS: template struct Vector { explicit Vector(int n); Vector operator *(ScalarType scalar) const; friend Vector operator *(ScalarType scalar, const Vector &v) { return v * scalar; } }; int main() { Vector f(3); Vector i(2); f * 2; 2 * f; i * 7; 7 * i; } > In this example, things won't link because I "forgot" to declare the > Vector friend function. By writing the function inside the class, I've wriiten it once for all types with which Vector is instantiated. > 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 And that was pretty much my point. People who know one language better than another will often think that the other language is incapable or clumsy at certain things. Often, this is not the case.