I'm currently undertaking a C++ course at university. I understand the general concept of shallow and deep copying using vectors however there's an example in my textbook that has me confused.
Please assume that it is a poorly implemented vector with no copy constructor defined so that it only performs a shallow copy of the data.
I understand what's happening in the first part
In the statement
vector<int> v2(v1);
vector
v1
is passed as a const reference argument to the vector copy constructor, sov1
can’t be changed, and the variablev2
is then initialized to a copy of the vectorv1
. Each data field will be copied, and any changes made later tov2
should not affectv1
. When the value inv1.the_data
is copied over, bothv1.the_data
andv2.the_data
will point to the same arrayBecause
v1.the_data
andv2.the_data
point to the same object, the statementv1[2] = 10;
also changes
v2[2]
. For this reason,v2
is considered a shallow copy ofv1
.
However I'm struggling to understand this part. I'm not quite sure why v2.num_items
won't also change in a shallow copy.
The statement
v1.push_back(20);
will insert
20
intov1[5]
and will changev1.num_items
to 6, but will not changev2.num_items
.
My current thoughts on it are that v1.the_data
and v2.the_data
are pointing to the same place in memory therefore they 'share' the same vector so that when 20 is added to the end of it both of the vectors should gain an additional integer.
I would greatly appreciate assistance in understanding why the number of items won't change for v2
when v1
is modified.