Suppose I create two vectors, one on the heap and one on the stack:
Vector<int> vector1;
Vector<int>* vector2 = new Vector<int>;
I then pass vector1
into two functions, say, foo1(Vector<int>)
and foo2(Vector<int>&)
. I also pass vector2
into foo3(Vector<int>*)
.
Since I'm new to C++, I'm rather confused by the difference in behaviours here.
Am I right to say that for
foo1
, the entirevector1
gets copied, and forfoo2
, only the reference tovector1
gets passed into the function?But isn't
vector1
, declared on the stack, supposed to be inaccessible anywhere else (i.e. from insidefoo2
) except the scope in which it was created?Also, does modifying the contents of
vector1
insidefoo1
,foo2
affect the original vector?- And is
vector1
automatically destroyed at the end of its scope, or do we have to delete it manually?