[comp.lang.c++] fifo queue

wgh@Grumpy.UUCP (William G. Hutchison) (09/30/88)

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_