Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > Re: String from...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 7 of 15 Topic 4202 of 4302
Post > Topic >>

Re: String from C to C++

by Jerry Coffin <jcoffin@[EMAIL PROTECTED] > Jul 15, 2008 at 12:23 PM

In article <2427530.qIh2yKrBSW@[EMAIL PROTECTED]
>, bart@[EMAIL PROTECTED]
 
says...

[ ... ] 

> operator=() should do two things:
> 1. Clean up the old contents of the object
> 2. create a copy of the contents of the other object and store that in
> the 'this' object.
> 
> The popular idiom to write operator=() is to use the copy-swap idiom:
> In pseudo-code the idiom goes like this:
>   FooBar::operator=(const FooBar& other)
>   {
>     FooBar temp(other) //create a new object that is a copy of 'other'
>     swap(*this, temp) //swap the contents of this and temp
>   } // as temp goes out of scope, let it destroy the old content of
> this.

The point of passing a reference to a const string is to avoid creating 
a copy -- but in this case, a copy is absolutely necessary anyway:

FooBar::operator=(FooBar temp) { 
	swap(*this, temp);
}

-- 
    Later,
    Jerry.

The universe is a figment of its own imagination.
 




 15 Posts in Topic:
String from C to C++
lau.dvd@[EMAIL PROTECTED]  2008-07-14 05:34:12 
Re: String from C to C++
"Daniel T." <  2008-07-14 09:05:20 
Re: String from C to C++
Bart van Ingen Schenau &l  2008-07-14 19:53:17 
Re: String from C to C++
lau.dvd@[EMAIL PROTECTED]  2008-07-14 19:40:10 
Re: String from C to C++
LR <lruss@[EMAIL PROTE  2008-07-15 01:49:45 
Re: String from C to C++
Bart van Ingen Schenau &l  2008-07-15 19:32:58 
Re: String from C to C++
Jerry Coffin <jcoffin@  2008-07-15 12:23:29 
Re: String from C to C++
Bart van Ingen Schenau &l  2008-07-16 17:43:55 
Re: String from C to C++
Francis Glassborow <fr  2008-07-15 09:03:38 
Re: String from C to C++
lau.dvd@[EMAIL PROTECTED]  2008-07-15 20:47:33 
Re: String from C to C++
Bart van Ingen Schenau &l  2008-07-16 17:53:34 
Re: String from C to C++
LR <lruss@[EMAIL PROTE  2008-07-16 13:32:02 
Re: String from C to C++
"Daniel T." <  2008-07-16 21:48:31 
Re: String from C to C++
lau.dvd@[EMAIL PROTECTED]  2008-07-17 19:09:56 
Re: String from C to C++
Bart van Ingen Schenau &l  2008-07-18 17:10:24 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 17:55:44 CDT 2008.