Path: utzoo!attcan!uunet!cs.utexas.edu!sdd.hp.com!elroy.jpl.nasa.gov!decwrl!nsc!pyramid!infmx!briand From: briand@infmx.UUCP (brian donat) Newsgroups: comp.lang.c++ Subject: Re: Beginner's Corner Summary: Feed Back on my first program in C++ Part 1 of 4 Keywords: First C++ Code. Message-ID: <4336@infmx.UUCP> Date: 26 May 90 00:16:43 GMT References: <4316@infmx.UUCP> Organization: Informix Software Inc., Menlo Park, CA. Lines: 133 // // 1 of 4 // // I thought that some of you might be curious as to what kind of // critcisms my linked list class recieved and further since the // criticism I did recieve was somewhat instructive, and further // still, since nobody flamed me for posting a heap of code, I've // decided to post version 2.00, with explanations attached // regarding improvements gleened from the critiques I recieved. // // First, I'd decided on-my-own, to break the original code out into // separate '.c' and '.h' files and achieved this just before the // first helpful advice arrived. // // Still further, some of that advice recommended creating STACK and // QUEUE classes, instead of writing functions which used class list // as a stack or a queue. // // So, the following files are now extant: // // vlist.h a virtural base class for list // vlist.c members for class VLIST // list.h class LIST derived from VLIST // stack.h class STACK derived from VLIST // stack.c members for class STACK // queue.h class QUEUE derived from VLIST // queue.c members for class QUEUE // testmain.c main() for testing purposes as before. // // Discussion continued below. ---------------------------------Cut HERE--------------------------------- /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // \\ // vlist.h Version 2.00 \\ // A linked-list object: May 25, 1990, Brian L. Donat \\ // \\ /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // // The following classes compose a very general doubly linked list, // which may be added to, inserted into, deleted from, and read from. // This size of list is maintained, allowing external algorithms to // perform finite operations on the list data. // /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ typedef int ELEMENT; ////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\ // NODE is a base class which allows for the // creation of a doubly linked list. class NODE { friend class VLIST; NODE *next, *previous; ELEMENT data; }; ////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\ // VLIST is the actual doubly linked list class // // [ NOTE: original comments deleted for brevity.] class VLIST { int listsize; NODE *head, *tail; NODE *cnode; // current NODE or copy NODE. public: VLIST(); ~VLIST(); virtual void headend(); virtual void tailend(); virtual void insert(ELEMENT E); virtual void add(ELEMENT E) { cnode = tail; insert(E); } virtual void remove(); virtual void step(); virtual void backstep(); virtual int find(ELEMENT E); virtual ELEMENT getval() const; virtual int getsize() const { return listsize; } }; ---------------------------------Cut HERE--------------------------------- // A lot more concise, eh? // One of the things I got hit for here was my style of 'commenting'. // /////\\\\\ <- One person believes that some compilers // might view the last '\' as an escape which // doom the next line to being commented out. // I tested this and can't prove it as a concern with Cfront 2.0 // Anybody got any insight on this? // Next, the whole class has been redefined as a virtual base class // so that others may use it flexibly via inheritance. // the previous definition had the following line: // // void add(ELEMENT E) { cnode = tail; list::insert(E); } // The 'list::' qualifier would of course not helped people // were they to copy this class via parameterization. // Beyond this, there were some suggestions about 'inlines'. // Is a line such as // foo() { cnode = (head->next) ? head->next : nptr; } // really too big to be an inline? // // What's a good rule of thumb for predicting the fulcrum // between memory usage for a function's stack // and that used by actual code inserted inline? // Does a programmer have to calculate it byte by byte? // // And there's another question, notice the member add(), -- // it has a function call within its declaration, is // the overhead for that call expanded inline too? // -- at what cost? // // Further, isn't the use of 'inline' a matter of determined choice // based on trade-offs in memory usage vs. run-time? // // -- infmx!briand@pyramid