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 method for overloading operator=?

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





PostPosted: Sat Feb 21, 2004 11:02 am    Post subject: lazy method for overloading operator=? Reply with quote



Hello, I have a quick little question. I want to overload the = op for a
class of mine, but I only need to do a deep copy for a single variable, and
I'm too lazy to assign each memeber by hand to get an initial shallow copy.
While I can just use memcpy to do the shallow copy and then do the deep
copy myself, apparently memcpy is slower for small operations than doing
them by hand, and I figure maybe there is a nice built in solution?

Any help is appreciated.

Jesse

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Sat Feb 21, 2004 6:11 pm    Post subject: Re: lazy method for overloading operator=? Reply with quote



In message <Xns9495ADCEAD240nonenonecom (AT) 199 (DOT) 45.49.11>, Jesse
<none (AT) none (DOT) com> writes
Quote:
Hello, I have a quick little question. I want to overload the = op for a
class of mine, but I only need to do a deep copy for a single variable, and
I'm too lazy to assign each memeber by hand to get an initial shallow copy.
While I can just use memcpy to do the shallow copy and then do the deep
copy myself, apparently memcpy is slower for small operations than doing
them by hand, and I figure maybe there is a nice built in solution?

Encapsulate all the shallow data in either a nested class or a base
class and use the generated assignment for that to largely automate the
shallow copying. But assuming memcpy() will actually work correctly for
what you are doing -- an assumption that needs careful checking -- does
the speed really matter? Unless you are doing a vast amount of
assignment in relation to everything else in your program I very much
doubt it.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jonathan Turkanis
Guest





PostPosted: Sat Feb 21, 2004 7:22 pm    Post subject: Re: lazy method for overloading operator=? Reply with quote




"Jesse" <none (AT) none (DOT) com> wrote

Quote:
Hello, I have a quick little question. I want to overload the = op
for a
class of mine, but I only need to do a deep copy for a single
variable, and
I'm too lazy to assign each memeber by hand to get an initial
shallow copy.
While I can just use memcpy to do the shallow copy and then do the
deep
copy myself, apparently memcpy is slower for small operations than
doing
them by hand, and I figure maybe there is a nice built in solution?

You might group the members for which the compiler generated
assignment would be okay into a helper class, and replace those
members with an instance of the helper. Then you can assign the helper
in one operation. Or you could put them in a base class.

Jonathan



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dhruv Matani
Guest





PostPosted: Sat Feb 21, 2004 7:25 pm    Post subject: Re: lazy method for overloading operator=? Reply with quote

On Sat, 21 Feb 2004 06:02:20 -0500, Jesse wrote:

Quote:
Hello, I have a quick little question. I want to overload the = op for a
class of mine, but I only need to do a deep copy for a single variable, and
I'm too lazy to assign each memeber by hand to get an initial shallow copy.
While I can just use memcpy to do the shallow copy and then do the deep
copy myself, apparently memcpy is slower for small operations than doing
them by hand, and I figure maybe there is a nice built in solution?

Just create a Implementation class say called Impl.

Simple run off the mill String class to demonstarate the uses:

struct string {
struct Impl {
int Length;
int/float/double/bool Other_Pod_Data;
};

Impl impl;
char *data;
//...

//Copy Ctor:
string (string const& _s) : impl(_s.impl) {
//Do a deep copy for the data part.
}

};



--
Regards,
-Dhruv.

Proud to be a Vegetarian.
http://www.vegetarianstarterkit.com/
http://www.vegkids.com/vegkids/index.html


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Steve Haigh
Guest





PostPosted: Sun Feb 22, 2004 1:42 am    Post subject: Re: lazy method for overloading operator=? Reply with quote

Jesse wrote:

Quote:
Hello, I have a quick little question. I want to overload the = op for a
class of mine, but I only need to do a deep copy for a single variable, and
I'm too lazy to assign each memeber by hand to get an initial shallow copy.
While I can just use memcpy to do the shallow copy and then do the deep
copy myself, apparently memcpy is slower for small operations than doing
them by hand, and I figure maybe there is a nice built in solution?
Not quite an answer as such, but have you considered autogenerating the

code? It sounds like you have a lot of members (or you are very very
lazy), depending on the situation it may be worth the effort to write a
script or another program to generate your code for you.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Allan Odgaard
Guest





PostPosted: Sun Feb 22, 2004 10:57 am    Post subject: Re: lazy method for overloading operator=? Reply with quote

Jesse <none (AT) none (DOT) com> wrote

Quote:
Hello, I have a quick little question. I want to overload the = op for a
class of mine, but I only need to do a deep copy for a single variable, and
I'm too lazy to assign each memeber by hand to get an initial shallow copy.
While I can just use memcpy to do the shallow copy and then do the deep
copy myself, apparently memcpy is slower for small operations than doing
them by hand, and I figure maybe there is a nice built in solution?

Rather than wrap all but your member which needs deep copy (as most
have suggested), you can also just wrap the member which need deep
copy, and overload operator= for the wrapper.

// assuming strdup() and free() works for NULL-pointers
struct StringWrapper
{
typedef StringWrapper self;
char* str;
StringWrapper (char* aStr = NULL) { str = strdup(aStr)); }
StringWrapper (self const& rhs) { str = strdup(rhs.str)); }
~StringWrapper () { free(str); }
self& operator= (self const& rhs) { if(this != &rhs)
{ free(str); str = strdup(rhs.str); }
}
operator char* () { return str; }
};

Now you can just wrap your strings with the above:

struct BigAndHeavy
{
int i, j, k, l, m, n;
T obj1, obj2, obj3, obj4, obj5;
StringWrapper str;

BigAndHeavy (char* someStr) : str(someStr) { ... }
...
};

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dylan Nicholson
Guest





PostPosted: Mon Feb 23, 2004 11:40 am    Post subject: Re: lazy method for overloading operator=? Reply with quote

[email]Duff (AT) DIKU (DOT) DK[/email] (Allan Odgaard) wrote in message news:<689e217.0402212128.7db3dbb4 (AT) posting (DOT) google.com>...
Quote:
Jesse <none (AT) none (DOT) com> wrote

Hello, I have a quick little question. I want to overload the = op for a
class of mine, but I only need to do a deep copy for a single variable, and
I'm too lazy to assign each memeber by hand to get an initial shallow copy.
While I can just use memcpy to do the shallow copy and then do the deep
copy myself, apparently memcpy is slower for small operations than doing
them by hand, and I figure maybe there is a nice built in solution?

Rather than wrap all but your member which needs deep copy (as most
have suggested), you can also just wrap the member which need deep
copy, and overload operator= for the wrapper.

I'm glad someone suggested this - it's certainly the only route I

would recommend. I'm a strong believer in using compiler-generated
code where-ver possible: it both cuts down on maintenance and
increases code readability. The real reason for allowing the compiler
to generate operator= for you is not laziness, but to avoid programmer
error. One of the worse bugs I ever found was because someone had
forgotten to update operator= when a new member was added. Likewise,
when implementing swap(), you have to be extra diligent that every
member is swapped (and swapped exactly once!). Compiler-generated
swap is a must, IMO. Which is not to say there should not be a way of
turning it off (or even that maybe explicit syntax is needed to
specific that you want it).

Dylan

[ 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.