Explanation why copy constructors are called when custom objects are map values


Wed Apr 14 2021 17:20:27 GMT+0000 (UTC)

Saved by @wowza12341


people.insert(make_pair(55, Person("Bob", 45)));

1. Person("Bob", 45) is passed by value to make_pair(), so it is coped. 3. map::insert() makes a copy of its arguments before storing them. Explanation form: STL containers always make copies of any data that is stored in them. The reason is that the original data could go out of scope at any time, without the container knowing about it. The only way for the container to have valid data is to make its own copy. Simple form: STL Containers make a copy of the original data that is in them. That is why copy constructors are used.