home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / gnu / g / help / 1703 < prev    next >
Encoding:
Text File  |  1993-01-25  |  2.9 KB  |  112 lines

  1. Path: sparky!uunet!pipex!warwick!uknet!sersun1!csc2!palmm
  2. From: palmm@csc2.essex.ac.uk (Palmer M J)
  3. Newsgroups: gnu.g++.help
  4. Subject: help!
  5. Keywords: Linker, ld, collect
  6. Message-ID: <7684@sersun1.essex.ac.uk>
  7. Date: 25 Jan 93 14:00:00 GMT
  8. References: <1993Jan21.004406.6508@informix.com>
  9. Sender: news@sersun1.essex.ac.uk
  10. Reply-To: palmm@essex.ac.uk (Palmer M J)
  11. Organization: University of Essex, Colchester, UK
  12. Lines: 98
  13.  
  14. I've recently been trying to get to grips with c++. I wrote the following
  15. small programme to implement a list template. However, it does not behave as
  16. I would have expected. If I define a list of chars or ints in main then all is
  17. ok. But defining the list to be of type char* does not work. If I insert a
  18. string item by typing 
  19.    i aaa
  20. then insert another string by typing
  21.    i bbb
  22. then type 
  23.    d
  24. to display the contents of the list, it prints out
  25.    bbb bbb
  26. If I then type
  27.    s ccc
  28. and then
  29.    d
  30. then what is printed out is 
  31.    ccc ccc ccc
  32. It seems that by reading in a string to the local char array item in main
  33. causes the value of each element in list to be changed to the value of the
  34. newly read in string. Could someone please put me out of my misery explain
  35. what is going on.
  36.  
  37. Thanks in advance.
  38. Mike Palmer.
  39.  
  40. PS. I am using gcc version 2.2.2
  41.  
  42. //////////////////////////////////////////////////////////////////////////////
  43. // List template definition
  44. //////////////////////////////////////////////////////////////////////////////
  45. #include <assert.h>
  46. #include <iostream.h>
  47.  
  48. enum Boolean { false = 0, true = 1 };
  49. template <class Type> class Item;
  50.  
  51. template <class Type> 
  52. class List {
  53.    public:
  54.       List() { firstItem = 0; }
  55.       ~List() {}
  56.       void insert(Type);
  57.       void display();
  58.    private:
  59.       Item<Type> *firstItem;
  60. };
  61.  
  62. template <class Type> 
  63. class Item {
  64.    friend class List<Type>;
  65.    private:
  66.       Item(Type value) { val = value; next = 0; }
  67.       Type val;
  68.       Item *next;
  69. };
  70.  
  71. template <class Type> 
  72. void List<Type>::insert(Type val) {
  73.    Item<Type> *pt = new Item<Type>(val);
  74.    assert( pt != 0 );   // catch free store exhaustion
  75.    pt->next = firstItem;
  76.    firstItem = pt;
  77. }
  78.  
  79.  
  80. template <class Type> 
  81. void List<Type>::display() {
  82.    for ( Item<Type> *pt = firstItem; pt; pt = pt->next )
  83.       cout << pt->val << " ";
  84.    cout << endl;
  85. }
  86.  
  87. //////////////////////////////////////////////////////////////////////////////
  88. // Define and test a list of strings.
  89. ////////////////////////////////////////////////////////////////////////////// 
  90. main () { 
  91.    typedef char *Type;    
  92.    List<Type> *list = new List<Type>; // create a list of strings
  93.    char ch;         // select test operation
  94.    char  item[10];    // input string buffer
  95.    while ( cin >> ch )
  96.       switch(ch) {
  97.      case 'q':
  98.         return 1;
  99.      case 's':
  100.             cin >> item;
  101.             cout << item << '\n';
  102.         break;
  103.          case 'i':
  104.             cin >> item;
  105.             list->insert(item);
  106.             break;
  107.          case 'd':
  108.             list->display();
  109.             break;
  110.       }
  111. }
  112.