I read C ++ Primer, 3rd Ed (Lippman and Lajoie), and he says that when a vector needs to be redistributed to create space for more elements added using push_back()
, the elements will be copied to a new space, and then the destructor is called old elements. I am confused why this is necessary - why data cannot be copied bit-by-bit? I believe the answer is related to dynamic memory allocation, but my current line of reasoning is that even if vector elements process dynamic memory, the data actually stored in the elements will be pointers, which means that bitwise copying will keep the location. which they point to and will not present any problems. I can see how reassigning the dynamically allocated memory pointed to by these elements will be a problem, as this will invalidate the pointers, but, as far as I can tell, there would be no reason to redefine the vector.
Can someone give me a simple example of a class that cannot be moved in half?
source share