home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12344 < prev    next >
Encoding:
Text File  |  1992-08-14  |  3.3 KB  |  142 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!gumby!wupost!eclnews!venus!abed
  3. From: abed@venus.wustl.edu (Abed M. Hammoud)
  4. Subject: Reference counting for vectors, (Tony Hansen's book).
  5. Message-ID: <1992Aug14.164719.9719@wuecl.wustl.edu>
  6. Sender: usenet@wuecl.wustl.edu (Usenet Administrator)
  7. Nntp-Posting-Host: venus
  8. Reply-To: abed@saturn.wustl.edu
  9. Organization: Washington University, ESSRL
  10. Date: Fri, 14 Aug 1992 16:47:19 GMT
  11. Lines: 129
  12.  
  13.  
  14. Hello, 
  15.  
  16. I was trying to implement the vector class using the method suggested
  17. by tony Hansen. i.e reference counting. the following code is taken
  18. from Hansen's book (C++ Answer book). There is some thing that doesn't
  19. seems correct. Please let me know what you think.
  20.  
  21. For example the way the code work is:
  22.  
  23. Assume you declare a vector of size 10 and initialize it to all 5:
  24.   
  25.     vector A(10, 5); 
  26.  
  27. Then assume you declare another vector B and set its elements equal
  28. to those of A, i.e you can say:
  29.  
  30.     vector B(10); B = A; 
  31.  
  32.     or 
  33.  
  34.     vector B = A; 
  35.  
  36. then lets say you wanted to change the contents of B. By doing so
  37. the contents of A also changes.
  38.  
  39. The following code is the test program that I used. I compiled it
  40. using g++ on a sun sparc machine please compile it and see for your
  41. self.
  42.  
  43. I think this is a very bad design. But I am not sure if there is something
  44. that I am missing. Could somebody please comment. 
  45.  
  46. By the way does any body know if Hansen's working on a new version of the
  47. book.
  48.  
  49. thanks, 
  50. +-------------------------------------------------+-----------------------+
  51. |Abed M. Hammoud (KB0INX)              | abed@saturn.wustl.edu |
  52. |Washington University.                  | Office:               |
  53. |Electronic Systems & Signals Research Laboratory.| -Voice:(314) 935-7547 |
  54. |Department of Electrical/Biomedical Engineering. | -FAX:  (314) 935-4842 |
  55. |Campus Box 1161, One Brookings Drive.          |              |
  56. |St. Louis, MO , 63130 USA              |                       |
  57. +-------------------------------------------------+-----------------------+
  58.  
  59.  
  60.  
  61.  
  62. /*-----------------cut here and save rest in foo.c------------------*/
  63. /*-----------------then compile and see for yourself  -------------*/
  64.  
  65. #include <iostream.h>
  66.  
  67. class vector
  68. {
  69.    struct vrep
  70.       {
  71.      float *f; 
  72.      int refcnt; 
  73.      int length; 
  74.       } *p; 
  75. public:
  76.    vector(int size, float init = 0.0); 
  77.    vector(vector& x); 
  78.    ~vector(); 
  79.    vector& operator = (vector& vec);
  80.    float& operator [] (int i); 
  81.    friend ostream& operator << (ostream&, vector&); 
  82. }; 
  83.  
  84. vector::vector(int size, float init)
  85. {
  86.    p = new vrep; 
  87.    p->f = new float [p->length = size]; 
  88.    for (int i = 0; i < size; i++)
  89.       p->f[i] = init; 
  90.    p->refcnt = 1; 
  91. }
  92.  
  93. vector::~vector()
  94. {
  95.    if (--p->refcnt == 0) {
  96.       delete p->f; 
  97.       delete p; 
  98.    }
  99. }
  100.  
  101. vector::vector(vector& x)
  102. {
  103.    x.p->refcnt++; 
  104.    p = x.p; 
  105. }
  106.  
  107. vector& vector::operator = (vector& vec) {
  108.    vec.p->refcnt++; 
  109.    if (--p->refcnt == 0) {
  110.       delete p->f; 
  111.       delete p; 
  112.    }
  113.    p = vec.p; 
  114.    return *this; 
  115. }
  116.  
  117. float& vector::operator[] (int i)
  118. {
  119.    return p->f[(i >= 0) && (i < p->length) ? i : 0]; 
  120. }
  121.  
  122. ostream& operator << (ostream& out, vector& v)
  123. {
  124.    out << v.p->length << endl; 
  125.  
  126.    float *f = v.p->f; 
  127.    int l = v.p->length; 
  128.    for (int i = 0; i < l; i++)
  129.       out << *f++ << endl; 
  130.    return out << endl; 
  131. }
  132.  
  133. main()
  134. {
  135.    vector A(5, 3); 
  136.    vector B = A; 
  137.    cout << A << B << endl; 
  138.  
  139.    cin >> B[2]; 
  140.    cout << A << B << endl; 
  141. }
  142.