Xref: utzoo comp.lang.fortran:1306 comp.lang.c++:1696 Path: utzoo!attcan!uunet!convex!killer!ames!mailrus!ncar!gatech!udel!burdvax!Grumpy!wgh From: wgh@Grumpy.UUCP (William G. Hutchison) Newsgroups: comp.lang.fortran,comp.lang.c++ Subject: Re: fifo queue (was Data types _without_ pointers) Summary: a more natural implementation in C++ Message-ID: <368@Grumpy.UUCP> Date: 30 Sep 88 15:53:53 GMT References: <1032@amelia.nas.nasa.gov> <4026@lanl.gov> <1038@amelia.nas.nasa.gov> <13711@mimsy.UUCP> Followup-To: comp.lang.fortran Organization: UNISYS CS, Blue Bell, PA Lines: 51 In article <13711@mimsy.UUCP>, chris@mimsy.UUCP (Chris Torek) writes: > In article <1038@amelia.nas.nasa.gov> fouts@lemming.nas.nasa.gov.nas.nasa.gov > [is there an echo.echo here? :-) ] (Marty Fouts) writes: > >... I can implement a fifo using an array and two integers .... > [Fortran example deleted] > [C example also deleted; the C version was buggy] > >Neither of these is a good substitute for: > > > >Declare fifo to be a fifo of real; > > > >and using 'fifo = x' to add x to the end of fifo and 'x = fifo' to > >remove x from the begining of the fifo. > > You just need to use a *real* language :-). > C++ gets rather close. The syntax is a bit peculiar: you will > have to write something like > > fifo(float) f; // uses a cpp macro hack to fake parameterised type > f.put(x); // add x to tail > x = f.get(); // get x from head Actually, C++ can do better than that! You can define a class fifo which overloads operator= with two functions overloading '=': one with arguments (float, fifo) and the other with arguments (fifo, float). Thus the syntax desired: f = x; // this means put float x onto fifo f, and x = f; // this means get float from head of f, and // assign it to float x will work exactly as you want, if you define fifo.h correctly in C++ ! You might also consider the alternative of leaving '=' as a plain vanilla assignment operator (to avoid confusing the poor programmer using the class!), but to overload << e.g. f << x; // this means put float x onto fifo f, and x << f; // this means get float from head of f, and // assign it to float x by analogy to cout << "i = " << i << "\n"; in stream.h. Wow! C++ includes the good stuff from Ada (TM) and Modula-2, but leaves out the dreck! Bravo! -- Bill Hutchison, DP Consultant rutgers!cbmvax!burdvax!Grumpy!wgh Unisys UNIX Portation Center "What one fool can do, another can!" P.O. Box 500, M.S. B121 Ancient Simian Proverb, quoted by Blue Bell, PA 19424 Sylvanus P. Thompson, in _Calculus Made Easy_