Reputation: 15069
I have a list<Myobj>
source which contains some Myobj
instances, if I assign it to an empty list<MyObj>
target what exactly happens in terms of contents?
Are Myobj
instances duplicated in target via shallow copy? is the copy contructor called? or are they not duplicated at all?
Upvotes: 2
Views: 120
Reputation: 477464
After the assignment, the left-hand side is semantically equivalent to what the right-hand side was before the assignment, provided the type Myobj
is sufficiently sane (e.g. its copy constructors and assignment operators also satisfy this if it's a class type).
Upvotes: 3
Reputation: 227538
All the objects in the first list are copied into the second list. It is better not to think about "shallow" and "deep" copying here. The elements are copied, and the "depth" of the copy depends on what the class' copy constructor or assignment operator do. If your type T were a plain pointer, then the pointers would get copied, but not what they point to. There is no special magic going on.
Note that there are some subtleties which depend on what the source, or RHS of the assignment is (thanks to @mooingduck for reminding me of this). In C++11, there are situations in which the contents of the RHS could be moved into the LHS, in which case there is no copying at all.
Upvotes: 9