I was trying to write user defined object with std::vector.
I read that for User Defined classes, if Copy Constructor and Assignment Operator are public then only one can insert its object in STL container.
This is because of two reasons::
- All STL contains always stores the copy of inserted objects, not the actual one. So, whenever we insert any element or object in container then it’s copy constructor is called to create a copy and then this copy is inserted into the container.
- While insertion in std::vector it might be possible that storage relocation takes place internally due to insufficient space. In such cases assignment operator will be called on objects inside the container to copy them from one location to another.
why all STL container always stores the copy of inserted objects, not the actual one?
I couldn't understand the reason as to why they didn't allow the storing of the actual object. what was the disadvantage?