 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Radu Grigore Guest
|
Posted: Wed Sep 24, 2003 8:24 pm Post subject: lazy copy |
|
|
Is it possible to write a class LazyCopy that can be used like in the
following code? Or at least something similar?
--------
class Obj
{
public:
Obj() {val = 0;}
Obj(const Obj& other) {val = other.val; }
void SetValue(int val_) {val = val_; }
int GetValue() const;
private:
int val;
}
LazyCopy<Obj> a(new Obj());
LazyCopy<Obj> b, c, d;
a.SetValue(10);
[1] b = c = d = a;
cout << c.GetValue() << endl; // writes 10
cout << a.GetValue() << endl; // writes 10
[2] c.SetValue(20);
cout << c.GetValue() << endl; // writes 20
cout << a.GetValue() << endl; // writes 10
-----------
At line [1] there is only one object Obj and all four identifiers a,
b, c, d refer to it. At line [2] there are two objects: one refered by
a, b, and d; the other refered by c.
In order to achive this you need to distinguish between a const method
(eg. GetValue) and a non-const method (eg. SetValue) somewhere in
LazyCopy. This is needed because a copy should be created whenever an
object from a collection (collection = one object, ref counted) is
mutated.
regards,
radu
http://www.geocities.com/_rgrig
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|