C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

lazy copy

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Radu Grigore
Guest





PostPosted: Wed Sep 24, 2003 8:24 pm    Post subject: lazy copy Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.