Can someone explain what exactly is going on here?
Yes. If you access the object , after push_back , resize or insert redistributed the contents of vector , this behavior is undefined, which means what actually happens before your compiler, your OS, what do some more stuff is and, possibly, a number of other factors, such like perhaps the phase of the moon, the humidity in some remote place ... you name it; -)
In short, this (indirectly through the implementation of std::vector ) calls the destructor of the object itself, so the object has expired. In addition, the memory previously occupied by the object was freed by the vector allocator. Therefore, the use of object non-static members leads to undefined behavior, because the this pointer passed to the function no longer points to the object. However, you can access the static members of the class:
struct F { static int i; static int foo(); double d; void bar();
Arne mertz
source share