Since you did not complete the assignment of copy A , preserving the bandwidth of the vector. The compiler generated copy assignment A , guarantees a single copy of the elements of the vector. A vector constructor-copier (and destination-copy) is not required to preserve the capacity of the original vector object. It copies elements while maintaining the size vector.
This means that if you define the copy destination A as:
A& operator = (const A & a) { data = a.data; }
It will print 0 anyway.
The behavior you want can be achieved by implementing the copy destination as:
A& operator = (const A & a) { data.reserve(a.capacity()); data.insert(data.end(), a.begin(), a.end()); }
Note that .insert() only copies the element due to which .size() changes, but .capacity remains unchanged. This is why you need to explicitly call .reserve() .
Nawaz source share