Copying is designed to initialize new objects by copying the contents of existing ones, the purpose is to rewrite existing objects with the contents of other objects - these two things are very different. In particular, this
SomeClass a;
SomeClass b = a;
- copy initialization - you copy ato create a new SomeClass called busing form syntax
T x = y;
SomeClass copy ( , , ). a; , .
SomeClass(const SomeClass& rhs)
: x(rhs.x)
{}
( , , , , .)
,
SomeClass c(a);
- . , , , , :
http://www.gotw.ca/gotw/036.htm
. :
http://www.gotw.ca/gotw/001.htm
,
b = c;
- . , b c ( , std::auto_ptr, , ). , - ( , , , ):
SomeClass& operator=(const SomeClass& rhs)
{
x = rhs.x;
return *this;
}
, , , , . . :
http://en.wikibooks.org/wiki/More_C++_Idioms/Copy-and-swap