home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!gumby!wupost!eclnews!venus!abed
- From: abed@venus.wustl.edu (Abed M. Hammoud)
- Subject: Reference counting for vectors, (Tony Hansen's book).
- Message-ID: <1992Aug14.164719.9719@wuecl.wustl.edu>
- Sender: usenet@wuecl.wustl.edu (Usenet Administrator)
- Nntp-Posting-Host: venus
- Reply-To: abed@saturn.wustl.edu
- Organization: Washington University, ESSRL
- Date: Fri, 14 Aug 1992 16:47:19 GMT
- Lines: 129
-
-
- Hello,
-
- I was trying to implement the vector class using the method suggested
- by tony Hansen. i.e reference counting. the following code is taken
- from Hansen's book (C++ Answer book). There is some thing that doesn't
- seems correct. Please let me know what you think.
-
- For example the way the code work is:
-
- Assume you declare a vector of size 10 and initialize it to all 5:
-
- vector A(10, 5);
-
- Then assume you declare another vector B and set its elements equal
- to those of A, i.e you can say:
-
- vector B(10); B = A;
-
- or
-
- vector B = A;
-
- then lets say you wanted to change the contents of B. By doing so
- the contents of A also changes.
-
- The following code is the test program that I used. I compiled it
- using g++ on a sun sparc machine please compile it and see for your
- self.
-
- I think this is a very bad design. But I am not sure if there is something
- that I am missing. Could somebody please comment.
-
- By the way does any body know if Hansen's working on a new version of the
- book.
-
- thanks,
- +-------------------------------------------------+-----------------------+
- |Abed M. Hammoud (KB0INX) | abed@saturn.wustl.edu |
- |Washington University. | Office: |
- |Electronic Systems & Signals Research Laboratory.| -Voice:(314) 935-7547 |
- |Department of Electrical/Biomedical Engineering. | -FAX: (314) 935-4842 |
- |Campus Box 1161, One Brookings Drive. | |
- |St. Louis, MO , 63130 USA | |
- +-------------------------------------------------+-----------------------+
-
-
-
-
- /*-----------------cut here and save rest in foo.c------------------*/
- /*-----------------then compile and see for yourself -------------*/
-
- #include <iostream.h>
-
- class vector
- {
- struct vrep
- {
- float *f;
- int refcnt;
- int length;
- } *p;
- public:
- vector(int size, float init = 0.0);
- vector(vector& x);
- ~vector();
- vector& operator = (vector& vec);
- float& operator [] (int i);
- friend ostream& operator << (ostream&, vector&);
- };
-
- vector::vector(int size, float init)
- {
- p = new vrep;
- p->f = new float [p->length = size];
- for (int i = 0; i < size; i++)
- p->f[i] = init;
- p->refcnt = 1;
- }
-
- vector::~vector()
- {
- if (--p->refcnt == 0) {
- delete p->f;
- delete p;
- }
- }
-
- vector::vector(vector& x)
- {
- x.p->refcnt++;
- p = x.p;
- }
-
- vector& vector::operator = (vector& vec) {
- vec.p->refcnt++;
- if (--p->refcnt == 0) {
- delete p->f;
- delete p;
- }
- p = vec.p;
- return *this;
- }
-
- float& vector::operator[] (int i)
- {
- return p->f[(i >= 0) && (i < p->length) ? i : 0];
- }
-
- ostream& operator << (ostream& out, vector& v)
- {
- out << v.p->length << endl;
-
- float *f = v.p->f;
- int l = v.p->length;
- for (int i = 0; i < l; i++)
- out << *f++ << endl;
- return out << endl;
- }
-
- main()
- {
- vector A(5, 3);
- vector B = A;
- cout << A << B << endl;
-
- cin >> B[2];
- cout << A << B << endl;
- }
-